Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to correctly call setListAdapter

Tags:

android

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.

like image 435
Luch Filip Avatar asked Aug 04 '13 17:08

Luch Filip


People also ask

What is ListActivity?

An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item. ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results.

How do I set up a list adapter?

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.


1 Answers

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.

like image 133
laalto Avatar answered Sep 20 '22 14:09

laalto