Khi dữ liệu được liên kết với bộ điều hợp mảng bị thay đổi, việc vô hiệu hóa chế độ xem danh sách có đủ để hiển thị các giá trị được cập nhật không? Đoạn mã sau không hoạt động, tôi đã hiểu nhầm điều gì ở đây.?
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();
}
}
Bố cục được sử dụng:
<?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>