我的应用程序有一个错误。我在我的列表视图中正确添加项目并自动刷新列表视图以从列表视图中删除项目它也可以正常工作,但是如果我添加项目并删除然后刷新不起作用。对不起我的英语如果有问题请在评论中提问.实际上我想在列表视图中添加项目并按下按钮,所以我希望列表视图刷新。所以我使用adapter.notifyDataSetChanged();
,如果我点击它从列表中删除的项目。我的问题是从列表中删除项目后自动刷新不起作用。XML 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
的Java代码:
public class Test extends ListActivity {
List<String> list = new ArrayList<String>();
String bbc = "";
String bbc2 = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button btn = (Button) findViewById(R.id.btn_test);
final Spinner aminika = (Spinner) findViewById(R.id.spinner1);
final EditText edit = (EditText) findViewById(R.id.edit_test);
//for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.all_test));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
aminika.setAdapter(dataAdapter);
//for listview
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
//=================================================
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//=====
bbc = String.valueOf(aminika.getSelectedItem());
bbc2 = edit.getText().toString();
list.add(bbc + " " + bbc2);
edit.setText("");
adapter.notifyDataSetChanged();
//''''''''''''''''''''''''''''''''''''''
}
});
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
//=============================
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Test.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to remove!")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
String item = (String) getListAdapter().getItem(position);
list.remove(item);
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
//=====================================
}
}
我的错误是什么?