brian
brian

Reputation: 6922

How to use notifyDataSetChanged

My project simplify as below: First, I use application method Data.java to save data. It contain the data:

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);
}

My AActivity class onCreate as below:

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);

And when button is click, startActivity the BActivity class. In BActivity class, the code as below:

Data d = (Data)this.getApplication();
d.adddata("newdata");
finish();

And AActivity class onResume() as below:

@Override
public void onResume(){
super.onResume();
this.DataAdapter.notifyDataSetChanged();
}

But why the list is not update? I confirm the data has be save.

My 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;
}
}

Upvotes: 1

Views: 19181

Answers (3)

Sprigg
Sprigg

Reputation: 3318

I think your problem is that your DataAdapter is referenced to the array test, but test never changes. Referencing data to the DataAdapter instead of test should work.

OK, after looking at the Adapter code it will not work. Why are you copying your data? The adapter will never notice a change in the data element, because it is only working with a copy of that element at construction time. If copying the data is necessary, you should make sure the adapter updates its content, too.

Upvotes: 0

Mark Mooibroek
Mark Mooibroek

Reputation: 7696

You can't access the notifyDataSetChanged as a static method
( thats what you are doing in your example ).

If you have a ListActivity: you have access to the method getListAdapter().

Thats the right reference to your dataset.

So in short:

getListAdapter().notifyDataSetChanged();

will do the trick. If you don't have a ListActivity then you will have to find your listview thru View.findViewById([id of listview]) and get the Adapter there.

Hope this helps a bit :-)

Upvotes: 0

gwvatieri
gwvatieri

Reputation: 5183

Add your new data directly to the adapter not to 'd'. The adapter keeps its own internal data which means that whatever changes you apply to your 'd' has no impact on the adapter.

For example:

List<String> itemsList = new ArrayList<String>();
ArrayAdapter aa = new ArrayAdapter(..., itemsList);
...
itemsList.add("new item"); --> wrong!
aa.notifyDataSetChanged(); --> nothing changes, you wrongly added the item to itemsList

you have to deal directly with the adapter:

aa.add("new item");          --> correct
aa.notifyDataSetChanged();   --> the adapter will reflect the change

Upvotes: 15

Related Questions