欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法

 更新時間:2016年01月04日 14:11:29   作者:傅榮康  
這篇文章主要介紹了Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法,實例分析了Android中ListView控件的使用技巧,需要的朋友可以參考下

本文實例講述了Android編程使用ListView實現(xiàn)數(shù)據(jù)列表顯示的方法。分享給大家供大家參考,具體如下:

要將數(shù)據(jù)庫中的數(shù)據(jù)列表顯示在屏幕上,我們要使用ListView這個控件,當用戶從數(shù)據(jù)庫中取出數(shù)據(jù)時,要將數(shù)據(jù)綁定到顯示控件上,如何綁定呢,我們需要創(chuàng)建適配器進行綁定,創(chuàng)建適配器有兩種方式:

第一種是用SimpleAdapter創(chuàng)建(要求綁定的數(shù)據(jù)是List<HashMap<String, Object>>數(shù)據(jù)類型)

第二種是用SimpleCursorAdapter創(chuàng)建(要求綁定的數(shù)據(jù)是Cursor數(shù)據(jù)類型)

顯示效果如圖所示:

界面布局:

item.xml

<?xml version="1.0" encoding="utf-8"?>
<!--item -->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <!-- 名稱 -->
 <TextView
  android:layout_width="130dp"
  android:layout_height="wrap_content"
  android:id="@+id/name"
 />
 <!-- 電話 -->
 <TextView
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:id="@+id/phone"
 />
 <!-- 存款 -->
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/amount"
 />
</LinearLayout>

main.xml

<?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:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <TextView
  android:layout_width="130dp"
  android:layout_height="wrap_content"
  android:text="姓名"
 />
  <TextView
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:text="電話"
 />
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="存款"
 />
</LinearLayout>
 <!-- ListView控件 -->
<ListView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/listView"
  />
</LinearLayout>

使用SimpleAdapter進行數(shù)據(jù)綁定

public class MainActivity extends Activity {
  private PersonService service;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    service = new PersonService(this);
    ListView listView = (ListView) this.findViewById(R.id.listView);
    //獲取到集合數(shù)據(jù)
    List<Person> persons = service.getScrollData(0, 10);
    List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
    for(Person person : persons){
      HashMap<String, Object> item = new HashMap<String, Object>();
      item.put("id", person.getId());
      item.put("name", person.getName());
      item.put("phone", person.getPhone());
      item.put("amount", person.getAmount());
      data.add(item);
    }
    //創(chuàng)建SimpleAdapter適配器將數(shù)據(jù)綁定到item顯示控件上
    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
        new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
    //實現(xiàn)列表的顯示
    listView.setAdapter(adapter);
    //條目點擊事件
    listView.setOnItemClickListener(new ItemClickListener());
  }
    //獲取點擊事件
  private final class ItemClickListener implements OnItemClickListener{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      ListView listView = (ListView) parent;
      HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);
      String personid = data.get("id").toString();
      Toast.makeText(getApplicationContext(), personid, 1).show();
    }
  }
}

使用SimpleCursorAdapter進行數(shù)據(jù)綁定

public class MainActivity extends Activity {
  private PersonService service;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    service = new PersonService(this);
    ListView listView = (ListView) this.findViewById(R.id.listView);
    //獲取游標
    Cursor cursor = service.getCursorScrollData(0, 10);
    //創(chuàng)建SimpleCursorAdapter適配器將數(shù)據(jù)綁定到item顯示控件上
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
        new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
    listView.setAdapter(adapter);
    //條目點擊事件
    listView.setOnItemClickListener(new ItemClickListener());
  }
  private final class ItemClickListener implements OnItemClickListener{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      ListView listView = (ListView) parent;
      Cursor cursor = (Cursor) listView.getItemAtPosition(position);
      String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
      Toast.makeText(getApplicationContext(), personid, 1).show();
    }
  }
}

注意:使用第二種方式在獲取數(shù)據(jù)集合時必須指定主鍵"_id"

希望本文所述對大家Android程序設計有所幫助。

相關文章

最新評論