Marcin
Marcin

Reputation: 13

How to change listview adapter in section setOnItemClickListener

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?

Upvotes: 1

Views: 2760

Answers (2)

Glendon Trullinger
Glendon Trullinger

Reputation: 4062

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.

Upvotes: 1

Aleadam
Aleadam

Reputation: 40391

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

Upvotes: 1

Related Questions