android:listview SimpleAdapter

来源:互联网 发布:isodd函数 python 编辑:程序博客网 时间:2024/05/07 16:16
 LVActivity.java
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.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class LVActivity extends Activity implements OnItemClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.list);List<Map<String, String>> l = new ArrayList<Map<String, String>>();Map<String, String> m1 = new HashMap<String, String>();m1.put("name", "zhangsan");m1.put("id", "1");Map<String, String> m2 = new HashMap<String, String>();m2.put("name", "lisi");m2.put("id", "2");Map<String, String> m3 = new HashMap<String, String>();m3.put("name", "wangwu");m3.put("id", "3");l.add(m1);l.add(m2);l.add(m3);ListView lv = (ListView) findViewById(R.id.lv);SimpleAdapter sa = new SimpleAdapter(this, l, R.layout.list_line,new String[] { "name", "id" }, new int[] { R.id.name, R.id.id });lv.setAdapter(sa);lv.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Toast.makeText(this, "you get number id= " + arg3, Toast.LENGTH_SHORT).show();}}
 list.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

 

list_line.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <TextView            android:id="@+id/name"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:layout_marginLeft="430dp"            android:background="#0000aa"            android:textSize="30sp" />        <TextView            android:id="@+id/id"            android:layout_width="200dp"            android:layout_height="wrap_content"             android:background="#00aa00"            android:textSize="30sp" />   </LinearLayout>