ListView实例(SimpleAdapter实现)

来源:互联网 发布:淘宝客服教程视频在线 编辑:程序博客网 时间:2024/06/05 18:55

main.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:<a href="http://lib.csdn.net/base/15" class="replace_word" title="undefined" target="_blank" style="color:#df3434; font-weight:bold;">android</a>="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >      <LinearLayout          android:layout_width="wrap_content"//注意一定要wrap_content          android:layout_height="wrap_content"//注意一定要wrap_content          android:orientation="horizontal" >          <TextView          android:layout_width="100dp"          android:layout_height="wrap_content"          android:text="ID" />          <TextView          android:layout_width="100dp"          android:layout_height="wrap_content"          android:text="NAME" />          <TextView          android:layout_width="100dp"          android:layout_height="wrap_content"          android:text="AGE" />      </LinearLayout>      <ListView          android:id="@+id/listview"          android:layout_width="wrap_content"          android:layout_height="fill_parent" >      </ListView>  </LinearLayout>  

item.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/id1"          android:layout_width="100dp"          android:layout_height="wrap_content"          />      <TextView          android:id="@+id/name"          android:layout_width="100dp"          android:layout_height="wrap_content"          />      <TextView          android:id="@+id/age"          android:layout_width="100dp"          android:layout_height="wrap_content"          />  </LinearLayout> 

MainActivity.java

package org.xiazdong.db;  import java.util.ArrayList;  import java.util.HashMap;  import java.util.List;  import java.util.Map;  import org.xiazdong.db.domain.Person;  import org.xiazdong.db.service.DBService;  import android.app.Activity;  import android.os.Bundle;  import android.util.Log;  import android.widget.ListView;  import android.widget.SimpleAdapter;  public class MainActivity extends Activity {      private ListView listView;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          listView = (ListView)this.findViewById(R.id.listview);          DBService service = new DBService(this);          List<Person> persons = service.pageQuery(0, 10);          List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();          for(Person person:persons){              HashMap<String,Object>map = new HashMap<String,Object>();              map.put("id", person.getId());              map.put("name", person.getName());              map.put("age", person.getAge());              data.add(map);          }          System.out.println(data);          SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age});  //data表示显示的数据,一个Map为一行,List<Map>表示多行  //R.layout.item表示一个item的布局  //new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上          listView.setAdapter(adapter);      }  }  
0 0
原创粉丝点击