android-ListActivity与HashMap的简单使用

来源:互联网 发布:excel数据下拉递增 编辑:程序博客网 时间:2024/05/17 21:44



package zhang.listActivity;import java.util.ArrayList;import java.util.HashMap;import android.app.ListActivity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;public class listActivity extends ListActivity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();        HashMap<String,String> map1= new HashMap<String,String>();        HashMap<String,String> map2= new HashMap<String,String>();        HashMap<String,String> map3= new HashMap<String,String>();                map1.put("user", "zhang");        map1.put("id", "192.168.0.0");                map2.put("user", "wang");        map2.put("id", "192.168.0.1");                map3.put("user", "li");        map3.put("id", "192.168.0.2");                list.add(map1);        list.add(map2);        list.add(map3);                 SimpleAdapter listApadter = new  SimpleAdapter(         this,list,R.layout.user,new String[]{"user","id"},         new int[]{R.id.user_name,R.id.user_ip});         setListAdapter(listApadter);    }    protected void OnListItemClick(ListView l,View v,int position,long id){    super.onListItemClick(l, v, position, id);    System.out.print(id+position);            }        }



主布局


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >        <LinearLayout     android:id="@+id/listLinearLayout"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><ListView android:id="@id/android:list"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:drawSelectorOnTop="false"    android:scrollbars="vertical"        />    </LinearLayout></LinearLayout>



list子项布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="10pt"        >    <TextView    android:id="@+id/user_name"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="10pt"    />       <TextView    android:id="@+id/user_ip"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="10pt"    android:gravity="right"           /></LinearLayout>