gpt4 book ai didi

Android ListView- setListAdapter 和 getListAdapter 出错?

转载 作者:行者123 更新时间:2023-11-29 14:38:55 26 4
gpt4 key购买 nike

我在 MainAvtivity 中有以下代码:

private DBOperations DataDBoperation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DataDBoperation = new DBOperations(this);
DataDBoperation.open();

List values = DataDBoperation.getAllUserSIMs();

// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);



}
public void addSim(View view) {

ArrayAdapter adapter = (ArrayAdapter) getListAdapter();

EditText text = (EditText) findViewById(R.id.editText1);
SIMsClass stud = DataDBoperation.addSIMs(text.getText().toString());

adapter.add(stud);

}

public void deleteSim(View view) {

ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
SIMsClass sim = null;

if (getListAdapter().getCount() > 0) {
sim = (SIMsClass) getListAdapter().getItem(0);
DataDBoperation.deleteSIMs(sim);
adapter.remove(sim);
}

}

public void addPassword(View view) {

ArrayAdapter adapter = (ArrayAdapter) getListAdapter();

EditText text = (EditText) findViewById(R.id.editText2);
PasswordClass stud = DataDBoperation.addPassword(text.getText().toString());

adapter.add(stud);

}

这是 layoun XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="113dp"
android:text="TextView" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:onClick="addSim"
android:text="Add New" />

<Button
android:id="@+id/deleteBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/addBtn"
android:layout_below="@+id/editText1"
android:onClick="deleteSim"
android:text="Delete First" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/deleteBtn" >
</ListView>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/addBtn"
android:ems="10" />

<Button
android:id="@+id/passwordBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@android:id/list"
android:layout_toRightOf="@+id/editText1"
android:onClick="addPassword"
android:text="@string/password" />

</RelativeLayout>

错误:
该方法是 setListAdapter(Adapter) 未定义类型 MainActivity。
对于 MainActivity 类型,方法是 getListAdapter(Adapter) 未定义。

最佳答案

您需要扩展 ListActivity 而不是 ActivitysetListAdapterListActivity 的一个方法。 getListAdapter

相同

http://developer.android.com/reference/android/app/ListActivity.html

关于Android ListView- setListAdapter 和 getListAdapter 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com