[color=red]例子一:[/color]
编写一个user.xml在/res/layout中
[color=red]例子二:[/color]
编写list_item.xml在/res/layout中
[color=red]例子三:[/color]
编写list_item.xml在/res/layout中
编写strings.xml在/res/values中
public class ListActivity extends android.app.ListActivity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map1 = new HashMap<String,String>();
Map<String,String> map2 = new HashMap<String,String>();
map1.put("name", "zhangsan");
map1.put("ip", "0.1");
map2.put("name", "lisi");
map2.put("ip", "0.2");
list.add(map1);
list.add(map2);
SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.user, new String[]{"name","ip"}, new int[]{R.id.user_id,R.id.user_ip});
setListAdapter(sa);
}
}
编写一个user.xml在/res/layout中
<?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="horizontal">
<TextView android:layout_height="wrap_content" android:id="@+id/user_id" android:layout_width="wrap_content" android:text="TextView" android:layout_weight="1"></TextView>
<TextView android:layout_height="wrap_content" android:id="@+id/user_ip" android:layout_width="wrap_content" android:text="TextView" android:layout_weight="1"></TextView>
</LinearLayout>
[color=red]例子二:[/color]
public class HelloListView extends ListActivity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa"
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
编写list_item.xml在/res/layout中
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
[color=red]例子三:[/color]
public class ListViewActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
}
编写list_item.xml在/res/layout中
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
编写strings.xml在/res/values中
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>