Luch Filip Luch Filip - 4 months ago 12
Android Question

how to correctly call setListAdapter

I've searched how to solve this problem a lot, but i did not get any result. the only help that i need is, please take a look at this question asked on stackoverflow and on answer;

here is the question from this link:

"my activity does not extend ListActivity so this line throws an error. setListAdapter(fileList); it is supposed to populate a listview..."

ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);


the answer to this question is:

Obtain the reference of your
ListView
using
findViewById(int)
. Then call the method
ListView.setAdapter(ListAdapter)
on that reference with your adapter as the parameter.

now here is my code:

ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);

setListAdapter(adapter);

}


considering the Q/A, could someone please explain how exactly do i write it in the correct way. Any help would be very much appreciated.

Answer

As explained in the linked question and in this question's comments, it goes like this:

ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
ListView lv = (ListView)findViewById(R.id.your_listview_id);
lv.setAdapter(adapter);

ListActivity is a specialized Activity that hosts a ListView and has some convenience methods for accessing and manipulating it. However, nothing prevents you from having a ListView in a non-ListActivity Activity, you just lose the convenience methods and will have to write some more code yourself, like above.

Comments