1

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;
}
}
brian
  • 6,802
  • 29
  • 83
  • 124
  • I try this.BookmarkAdapter = new NASFilesAdapter(this, d); this.setListAdapter(BookmarkAdapter); it reset listadapter again to show list. Have any disadvantage about do this? – brian Dec 23 '11 at 08:52

3 Answers3

15

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
gwvatieri
  • 5,133
  • 1
  • 29
  • 29
  • How to add new data directly to the adapter? – brian Dec 23 '11 at 07:32
  • Why I can't use the aa.add method? – brian Dec 23 '11 at 07:36
  • What kinda of adapter are you extending? Guess DataAdapter is a custom adapter... – gwvatieri Dec 23 '11 at 07:38
  • I also try to add Data d = (Data)this.getApplication(); String test = new String[d.getsize()]; for(i = 0; i < d.getsize(); i++) { test[i] = d.getdata(i); } in onResume(). – brian Dec 23 '11 at 07:38
  • Oh ok, if you are using base adapter, you don't have the method add. When you use a base adapter you decide which data structure will keep your data, so you always need to modify that. You should post also the adapter code... – gwvatieri Dec 23 '11 at 07:46
  • I'm using arrayAdapter and use add() method to add item to customGridView. But when I call this method it sets position to first item of customGridView. same thing happens with notifyDataSetChanged(). I dont't want to refresh list instead just add items below current items and list should remain as it is(on it's current item position). – MobileEvangelist May 15 '13 at 11:13
  • Important thing you should not call notifyDataSetChanged() every time you add item to adapter (It's very costly to call this method repeatedly instead call when you complete adding items).. Hope this helps to improve performance........ – MobileEvangelist May 15 '13 at 11:17
0

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

Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
0

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.

Sprigg
  • 3,340
  • 1
  • 18
  • 26