Provide xml layout for ListView Item : ListView « UI « Android






Provide xml layout for ListView Item

   
package app.test;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Test extends Activity {

  private List<Map<String, Object>> data;
  private ListView listView = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PrepareData();
    listView = new ListView(this);

    SimpleAdapter adapter = new SimpleAdapter(this, data,
        R.layout.main, new String[] { "A", "B" }, new int[] {
            R.id.mview1, R.id.mview2 });
     listView.setAdapter(adapter);
    setContentView(listView);  
    
    OnItemClickListener listener = new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {
        setTitle(parent.getItemAtPosition(position).toString());
      }
    };
    listView.setOnItemClickListener(listener);
  }

  private void PrepareData() {
    data = new ArrayList<Map<String, Object>>();
    Map<String, Object> item;
    item = new HashMap<String, Object>();
    item.put("1", "A");
    item.put("2", "B");
    data.add(item);
    item = new HashMap<String, Object>();
    item.put("3", "C");
    item.put("4", "D");
    data.add(item);
    item = new HashMap<String, Object>();
    item.put("5", "E");
    item.put("6", "F");
    data.add(item);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TextView android:id="@+id/mview1" android:layout_width="100px"
    android:layout_height="wrap_content" />

  <TextView android:id="@+id/mview2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

   
    
    
  








Related examples in the same category

1.Using ExpandableListView
2.Using ListView
3.Using SimpleAdapter to fill data to ListView
4.Custom cell Renderer for ListView
5.Fill contact information to ListView
6.ListView.CHOICE_MODE_MULTIPLE
7.Use AbsListView OnScrollListener(AbsListView.OnScrollListener), AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the first letter of the visible range of cheeses.
8.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView.
9.FileList extends ListView
10.set ListView Height Based On Children
11.Adding a List
12.Lunch List
13.Get Item index in item click event
14.On nothing selected event
15.Create List