Android入門 ListView SimpleAdapter實現
阿新 • • 發佈:2018-11-11
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
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(){ @Override public 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返回Map Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show(); } };listView.setOnItemClickListener(listener);
SimpleCursorAdapter:
Cursor cursor = (Cursor)lv.getItemAtPosition(position);