android¿Ø¼þ¿ª·¢ÖListView
android¿Ø¼þ¿ª·¢ÖListView
±¾ÎÄÖ÷Òª½²Êö°²×¿¿ª·¢ÖеÄListView¿Ø¼þµÄʹÓ÷½·¨
java´úÂ룺package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ÉùÃ÷list£¬ÇÒlistÖеĵ¥ÔªÊÇmap
ArrayList> list = new ArrayList>();
//ÉùÃ÷map£¬ÇÒmapÖеÄÔªËØÊÇString¼ü值¶Ô
HashMap map1 = new HashMap();
HashMap map2 = new HashMap();
HashMap map3 = new HashMap();
//ÏòmapÖÐput¼ü值¶ÔÊý¾Ý
map1.put("name", "zhangsan");
map1.put("ip", "192.168.1.1");
map2.put("name", "lisi");
map2.put("ip", "192.168.1.2");
map3.put("name", "wangwu");
map3.put("ip", "192.168.1.3");
//½«mapÌí¼Óµ½listÖÐ
list.add(map1);
list.add(map2);
list.add(map3);
//Éú³ÉadapterÊÊÅäÆ÷
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[]{"name","ip"},
new int[]{R.id.name,R.id.ip});
setListAdapter(listAdapter);
}
//¸ølist°ó¶¨¼àÌýÆ÷£¬µ±°´ÏÂlistÖÐijһ¸öµ¥ÔªÊ±£¬¾Í»áÏàÓ¦´Ëº¯Êý
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id = " + id);
System.out.println("position = " + position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Ö÷²¼¾ÖÎļþmail.xml´úÂ룺
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical">
user.xml´úÂëʵÏÖ£º
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="UselessParent" >
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="20dip"
android:gravity="center_vertical"/>
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"/>
ʵÏÖЧ¹ûÈçÏ£º
<†·Ÿ"http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgo8c3Ryb25nPrXju/fP4LnYtcRsaXN0wdCx7aOs1NpMb2dDYXTW0LvhyuSz9s/gudi1xGxpc3TQxc+iyOfPwqO6PC9zdHJvbmc+CjxzdHJvbmc+PGltZyBzcmM9"" alt="">