Android入門:ListView(SimpleAdapter實現)

來源:互聯網
上載者:User

ListView是類似於將一個介面分為一行一行,如:

注意:listView.getItemAtPosition(int pos)內部調用了adapter.getItem(int position)方法,而每種適配器返回的類型都是不一樣的:

當SimpleAdapter返回Map<String,Object>

SimpleCursorAdapter返回Cursor;

繼承BaseAdapter返回自己實現的類型;

一般ListView都是用來顯示列表的,一般列表的資料都是來自資料庫的,因此我們這裡假設前面已經實現了一個DBService類,裡面存在pageQuery(int offset,int length);

比如dbservice.pageQuery(3,5);表示跳過3個記錄,插入5條記錄;

main.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: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);}}

總結:SimpleAdapter不需要內部實現Adapter,只能實現每個item布局都一樣的列表;

二、設定每個Item的監聽器

SimpleAdapter:

private OnItemClickListener listener = new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,//parent就是ListView,view表示Item視圖,position表示資料索引long id) {ListView lv = (ListView)parent;HashMap<String,Object> person = (HashMap<String,Object>)lv.getItemAtPosition(position);//SimpleAdapter返回MapToast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show();}};listView.setOnItemClickListener(listener);

SimpleCursorAdapter:

Cursor cursor = (Cursor)lv.getItemAtPosition(position);



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.