现在的位置: 首页 > 移动开发 > 正文

Android学习笔记—19_采用ListView实现数据列表显示,以及各种适配器使用,和如何写自己的适配器

2019年09月19日 移动开发 ⁄ 共 9271字 ⁄ 字号 评论关闭
 
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" 
        />
 <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的应用主页面显示;
  都是利用的适配器来实现显示的;
---------------------------------------

抱歉!评论已关闭.