當與陣列適配器關聯的數據更改時,使列表視圖失效足以顯示更新的值?以下代碼無法正常工作,我是否在這裏誤解了某些內容?適配器數據更改時更新列表視圖
public class ZeroItemListActivity extends Activity {
private ArrayList<String> listItems=new ArrayList<String>();
private ListView mMyListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMyListView=(ListView) findViewById(R.id.MyListView);
mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
}
public void addItem(View v){
listItems.add("list Item");
mMyListView.invalidate();
}
}
佈局使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/MyListView"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/AddItemsButton"
android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>
@blindstuff如果我有那麼我有什麼做的 – Developer 2013-08-21 05:48:36
如果BaseAdapter不存在更新列表視圖中的單個項目,notifyDataSetChanged()不會顯示出來,所以你需要改變'BaseAdapter '到'YourCustomAdapter' – 2014-03-20 15:22:52