我的項目簡化如下: 首先,我使用應用程序方法Data.java來保存數據。 它包含的數據:如何使用notifyDataSetChanged
private ArrayList<String> data = new ArrayList<String>();
public int getsize() {
return this.data.size();
}
public String getdata(int i) {
return this.data.get(i);
}
public void adddata(String s) {
return this.data.add(s);
}
我AActivity類的onCreate如下:
Data d = (Data)this.getApplication();
String test = new String[d.getsize()];
for(i = 0; i < d.getsize(); i++) {
test[i] = d.getdata(i);
}
//to show in list
DataAdapter = new DataAdapter (this, test);
setListAdapter(DataAdapter);
當按鈕被點擊,startActivity的BActivity類。 在BActivity類,如下面的代碼:如下
Data d = (Data)this.getApplication();
d.adddata("newdata");
finish();
而且AActivity類的onResume():
@Override
public void onResume(){
super.onResume();
this.DataAdapter.notifyDataSetChanged();
}
但是,爲什麼列表不更新? 我確認數據已保存。
我的DataAdapter:
public DataAdapter(Context ctxt, String[] d) {
this.data = new String[d.length];
myInflater = LayoutInflater.from(ctxt);
int i;
for(i = 0; i < d.length; i++) {
data[i] = d[i];
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewTag viewTag;
if(convertView == null) {
convertView = myInflater.inflate(R.layout.row_bookmark_list, null);
viewTag = new ViewTag((TextView)convertView.findViewById(R.id.tv));
convertView.setTag(viewTag);
}
else {
viewTag = (ViewTag) convertView.getTag();
}
viewTag.tv.setText(data[position]);
}
class ViewTag {
TextView tv;
public ViewTag(TextView t) {
this.tv = t;
}
}
我試試this.BookmarkAdapter = new NASFilesAdapter(this,d); \t this.setListAdapter(BookmarkAdapter);它再次重置listadapter以顯示列表。這樣做有什麼不利嗎? – brian