19_采用ListView實現數據列表顯示
--------------------------------------------------
ListView顯示界面的設置:
--------------------------
姓名 電話 存款
老周 12345687895 41111
老方 12515466874 5000
------------------------------
1.在使用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});這個SimpleCursorAdapter適配器的時候:
出現這個異常:
-----------------------------------------------
03-12 23:23:22.934: E/AndroidRuntime(23439): Caused by:
java.lang.IllegalArgumentException: column '_id' does not exist
--------------------------------------------------------------------
這個異常跟SimpleCursorAdapter ,這個源碼有關:
可以查看源碼,會有說明:
---------------------------
方法:1.把數據庫表中的主鍵id改成:_id
2.處理查詢後的結果集:主鍵起一個別名:
select personid as _id,name,phone,amount from person order by personid asc
limit ?,?
----------------------------------------------
1.使用android自帶的兩種適配器,實現listview顯示的代碼:
在DBSQLIte項目的基礎上,復制一份,並重新命名:ListViewDBSQLIte
2./ListViewDBSQLIte/src/com/credream/db/DBSQLIteActivity.java
package com.credream.db;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.security.auth.PrivateCredentialPermission;
import com.credream.adapter.PersonAdapter;
import com.credream.entity.Person;
import com.credream.service.PersonService;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class DBSQLIteActivity extends Activity {
private ListView listView;
private PersonService personService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personService=new PersonService(this);
listView=(ListView)this.findViewById(R.id.listView);
//給每個條目設置監聽對象
listView.setOnItemClickListener(new ItemClickListener ());
//show();
//測試使用SimpleCursorAdapter 適配器的時候用這個
//show2();
//測試使用自定義適配器的時候,用這個
show3();
}
final class ItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
// AdapterView<?> parent指的是用戶點擊的一個view對象, View view就是點擊的那個view
控件, int position就是所點擊的那一條值,在list中的索引值, long id就是listview在內
部進行排序用的
ListView lvView=(ListView)parent;
//使用自定義的適配器使用方法:
Person person=(Person)lvView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();
//使用cursor,也就是show2()的時候,cursor適配器的用法
//SimpleCursorAdapter 適配器來顯示listView控件
/*Cursor cursor=(Cursor)lvView.getItemAtPosition(position);
int personid=cursor.getInt(cursor.getColumnIndex("_id"));
Toast.makeText(getApplicationContext(), personid+"", 1).show();*/
}
}
private void show()
{
List<Person> persons=personService.getScrollData(0, 20);
List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
for(Person person:persons){
HashMap<String, Object> item=new HashMap<String, Object>();
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("amount", person.getAmount());
data.add(item);
}
//適配器:SimpleAdapter 和 item.xml中的條目顯示
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});
//
data,R.layout.item,把數據data綁定到R.layout.item上,也就是item.xml
指定的格式
//把name 這個key指向的值,指定到name顯示控件上
listView.setAdapter(adapter);
/*SimpleAdapter 的內部工作原理,將data當然以hashMap的形式,讓這些數據按照,key和R.id
中的控件對應的形式顯示
內部顯示的實現過程:
* int total=adapter.getCount();
//取得總素,然後迭代,然後調用View顯示,(顯示的時候獲取屏幕高度,然後獲取每個顯示控件
的高度,然後計算出每頁顯示多少個項)
int perpage=7;//
for(int i=0;i<perpage;i++){
View view=adapter.getView(i, , parent);
//用於得到條目的view對象
*/
}
//SimpleCursorAdapter 適配器來顯示listview
private void show2(){
Cursor cursor=personService.getCursorScrollData(0, 20);
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);
}
//采用自定義適配器來實現數據的綁定
private void show3(){
List<Person> persons=personService.getScrollData(0, 20);
PersonAdapter adapter=new PersonAdapter(this, persons,R.layout.item);
listView.setAdapter(adapter);
}
}
--------------------------------------------
2.建立一個自定義的適配器:來實現顯示數據效果:
/ListViewDBSQLIte/src/com/credream/adapter/PersonAdapter.java
package com.credream.adapter;
import java.util.List;
import com.credream.db.R;
import com.credream.entity.Person;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class PersonAdapter extends BaseAdapter
{
private List<Person> persons;//在綁定的數據
private int resouce;//綁定條目界面
private LayoutInflater inflater;//布局填充器,是系統的,所以要使用到系統上下文
public PersonAdapter(Context context,List<Person> persons, int resouce)
{
this.persons = persons;
this.resouce = resouce;
inflater=(LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return persons.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return persons.get(position);
}
//當顯示的時候listView有緩存功能,在訪問第一頁的時候,他會為每一個條目new一個對象
//然後,
@Override
public long getItemId(int position)
{
return position;
}
//View convertView這個如果第一次的話,就沒有緩存傳過來的事null
//如果不是第一次的話,那麼傳過來的應該不是null,含有以前緩存的條目
//這樣寫性能不好,因為每次的顯示條目的時候都要,調用這個方法,都會執行,三個控件的賦
值
@Override
public View getView(int position, View convertView, ViewGroup parent)
{TextView nameView=null;
TextView phoneView=null;
TextView amountView=null;
if(convertView==null){
//創建view對象
convertView= inflater.inflate(resouce, null);//指定用哪
一個resource的xml文件來生成一個view對象
//給控件賦值
nameView=(TextView)convertView.findViewById
(R.id.name);
phoneView=(TextView)convertView.findViewById
(R.id.phone);
amountView=(TextView)convertView.findViewById
(R.id.amount);
ViewCache cache=new ViewCache();
cache.nameView=nameView;
cache.phoneView=phoneView;
cache.amountView=amountView;
convertView.setTag(cache);
}else{
ViewCache cache=(ViewCache)convertView.getTag();
nameView=cache.nameView;
phoneView=cache.phoneView;
amountView=cache.amountView;
}
//性能沒有優化的情況
/*TextView nameView=(TextView)convertView.findViewById
(R.id.name);
TextView phoneView=(TextView)convertView.findViewById
(R.id.phone);
TextView amountView=(TextView)convertView.findViewById
(R.id.amount);*/
Person person=persons.get(position);
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return convertView;
}
private final class ViewCache{
//在android中使用public比較多,因為,使用public,不用多次創
建,private,每次自己使用的時候都會創建,public
//創建一次,就可以共享
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}
}
------------------------------------------
/ListViewDBSQLIte/res/layout/item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="xxxxx"
/>
<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>
-----------------------------------------------------
/ListViewDBSQLIte/res/layout/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"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/phone" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"/>
</LinearLayout>
----------------------------------------------------------
注意這裡將item.xml中除了命名空間的代碼考到main.xml中
然後修改這個地方就可以,給listview顯示的數據加標題了:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/name"
/>www.2cto.com
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/phone" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/amount" />
</LinearLayout>
-----------------------------------------------
3.還有<GridView>控件的使用,網格顯示控件,比如android的應用主頁面顯