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