我的應用程序有一個錯誤。我在我的列表視圖中正確添加項目並自動列出視圖刷新以從列表視圖中刪除項目視圖它的工作正確,但如果我添加項目並刪除然後刷新不工作。如果有問題請聯繫我的英語,請在評論中提問事實上,我想在列表視圖中添加項目,並按下按鈕,所以我期望刷新列表視圖。所以我使用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();
//=====================================
}
}
什麼是我的錯?
現在非常感謝它的工作。 – Aminika