1

I have listview with some listadapter after click on item from list I'd like to reload list with new data, change adapter's contests.

I use this in setOnItemClickListener, but I get error: "Type The constructor ArrayAdapter(new AdapterView.OnItemClickListener(){}, int, ArrayList) is undefined"

list.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1,Data.user.getChosen_rest()));

How should I do this correctly?

Gero
  • 1,842
  • 25
  • 45
Marcin
  • 13
  • 1
  • 3

2 Answers2

1

The answer here should get you headed in the right direction. If you can transform your initial ArrayAdapter using add(), remove(), insert(), or clear(), you can call notifyDataSetChanged() to reload it.

Edit

Actually, to better address your error, try replacing

list.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1,Data.user.getChosen_rest()));

with

list.setAdapter(new ArrayAdapter<String>(NameOfActivity.this, R.layout.simple_list_item_1,Data.user.getChosen_rest()));

first.

Community
  • 1
  • 1
Glendon Trullinger
  • 4,052
  • 1
  • 28
  • 35
  • It is, depending on where is the listener. I added the same approach before seeing your edit. – Aleadam Jun 06 '11 at 23:31
  • When I clear my adapter and try to add new string or "Re-create the ArrayAdapter with the new List data" I get error mentioned at beginning `Type The constructor ArrayAdapter(new AdapterView.OnItemClickListener(){}, int, ArrayList) is undefined"` – Marcin Jun 06 '11 at 23:34
  • BTW, `NameOfJavaFile` could be the name of the class extending OnClickListener, it's not clear that yo're referring to the Activity. – Aleadam Jun 06 '11 at 23:53
1

Glendon's answer is probably the right one. Nevertheless, in case you really need to change the adapter for some reason, you need to pass the right Context instance to the first argument of the constructor.

You can extend OnClickListener and add a field mContext to it, to which you will assign the current Activity. Then , use it as:

list.setAdapter(new ArrayAdapter<String>(mContext, R.layout.simple_list_item_1,Data.user.getChosen_rest()));

Alternatively, if the listener is an inner class of the Activity, you can use:

list.setAdapter(new ArrayAdapter<String>(MyActivity.this, R.layout.simple_list_item_1,Data.user.getChosen_rest()));
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • thanks Aleadam your answer works for me:) And one more question after click my adapter and things on list are changing. I'd like to have option go back after press back button, what is the easiest way to get done this task? – Marcin Jun 06 '11 at 23:47
  • I'm not really sure what you mean by go back. Anyhow, that looks like a completely different question. Please, consider posting a new one with a better description of what you need. – Aleadam Jun 06 '11 at 23:50