Custom List Item in Android ListView

I played with the tutorial on working with sections here:

http://developer.android.com/resources/tutorials/views/hello-listview.html

which says you are starting a List extension.

by public class Main extends ListActivity { 

This is based on bloating only the textview layout.

  <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" > </TextView> 

If I want to further customize the layout by adding images and an additional linear layout above the list, etc., can this method be used - if so, then how to do it?

+6
source share
1 answer

This is possible using the SimpleAdapter .

Here is an example:

  // Create the item mapping String[] from = new String[] { "title", "description" }; int[] to = new int[] { R.id.title, R.id.description }; 

Now, "title" is displayed on R.id.title and "description" on R.id.description (defined in XML below).

  // Add some rows List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("title", "First title"); // This will be shown in R.id.title map.put("description", "description 1"); // And this in R.id.description fillMaps.add(map); map = new HashMap<String, Object>(); map.put("title", "Second title"); map.put("description", "description 2"); fillMaps.add(map); SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to); setListAdapter(adapter); 

This is the corresponding XML layout called row.xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/description" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout> 

I used two TextViews, but it works the same with any view.

+15
source

Source: https://habr.com/ru/post/904034/


All Articles