listview.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" >
<LinearLayout
android:id="@+id/listLinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
</LinearLayout>
user.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="horizontal">
<TextView
android:id="@+id/user_name"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingTop="2dip"
android:textSize="10pt"/>
<TextView
android:id="@+id/user_ip"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="10pt"/>
</LinearLayout>
ListViewMain.java:
package com.example.wenandroid;
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 ListViewMain extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
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>();
map1.put("user_name", "张三");
map1.put("user_ip", "192.168.0.110");
map2.put("user_name", "李四");
map2.put("user_ip", "192.168.0.111");
list.add(map1);
list.add(map2);
SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip});
setListAdapter(listAdapter);
}
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l, v, position, id);
System.out.println("id="+id);
System.out.println("position="+position);
}
}
listview实现大纲简介样式:
package com.example.foreveross.office;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.wenandroid.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class ListViewSimple extends Activity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewsimple);
listview=(ListView)findViewById(R.id.listview);
List<Map<String,String>> data=new ArrayList<Map<String,String>>();
Map<String,String> item=new HashMap<String,String>();
item.put("name", "语文");
item.put("address", "语文是每个中国人都学习的一门学科");
data.add(item);
Map<String,String> item2=new HashMap<String,String>();
item2.put("name", "数学");
item2.put("address", "数学能提高人的逻辑思维");
data.add(item2);
listview.setAdapter(new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[]{"name","address"}, new int[]{android.R.id.text1,android.R.id.text2}));
}
}