我在 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)。