我在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>
错误: 的
对于MainActivity类型,该方法的setListAdapter(Adapter)未定义
对于MainActivity类型,该方法是未定义的getListAdapter(Adapter)。
答案 0 :(得分:5)
您需要扩展ListActivity
而不是Activity
。 setListAdapter
是ListActivity
的一种方法。 getListAdapter
http://developer.android.com/reference/android/app/ListActivity.html
答案 1 :(得分:2)
如果你只扩展活动,你需要从你设置为setcontentview的视图中获取listview
及其bttr设计和gve id(用于理解多列表标识)在xml布局中的listview,如
<ListView
android:id="@+id/listScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/deleteBtn" >
</ListView>
从您设置适配器的活动需要获取listview 然后将适配器设置为listview。 u gt error bcz tht method u cn override如果你扩展ListActivity或ListFragment。 bt ASFIK使用的简单活动
所以一旦尝试
@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.
// get your listview from layout "(R.layout.activity_main)"
// i hve gvn own id not android default .. for undestanding. you can use the id which u used
ListView list = findViewById(R.id.listScreen);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, values);
list.setAdapter(adapter);
}