I'm new to Android and I think I'm trying to do something really basic: I have a 5 strings in my Array (say 'One', 'Two', ...). I want to add these 5 strings to my list view in my listactivity.
My List:
<ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
My List Row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/homeItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>
Basically, I want to bind the Array items to the TextView homeItemName. I might add other items in my row later, so I can't just bind the listview to the entries.
Thanks!
To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.
android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .
As mentioned by Klap "android.R.layout.simple_list_item_1 is a reference to an built-in XML layout document that is part of the Android OS" All the layouts are located in: sdk\platforms\android-xx\data\res\layout. To view the XML of layout : Eclipse: Simply type android.
Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.
For code, take a quick look at this step-by-step tutorial
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); ListView lv = getListView();
It shows a basic implementation of an ArrayAdapter:
R.layout.list_item : is the xml layout (list_item.xml) that will be used for every ROW of your listview. COUNTRIES is the array of Strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With