android中使用ListView显示列表,共有三种实现方式:
分别是通过继承baseAdapter,直接使用simpleAdapter和simplecursoradapter三种方式
下面我们直接通过代码来看一下实现的过程,代码中有详细的注释,所以,不再详细介绍三种方法的实现方式、
在实现三种方式之前,先定义一个xml文件来显示每一个item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:textSize="30sp"
android:layout_width="0sp"
android:id="@+id/personid"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:textSize="30sp"
android:layout_weight="3"
android:layout_width="0sp"
android:id="@+id/name"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="0sp"
android:layout_weight="2"
android:textSize="30sp"
android:id="@+id/balance"
android:layout_height="wrap_content"
/>
</LinearLayout>
第一种:baseadapter
package com.example.sqllite;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BaseAdapterActivity extends Activity {
ListView list;
List<Person> persons = new ArrayList<Person>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) this.findViewById(R.id.list);
// 查询数据库获得person的集合
PersonDAO dao = new PersonDAO(getApplicationContext());
persons = dao.queryAll();
//为list添加adapter
list.setAdapter(new MyAdapter());
//添加监听事件
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//parent是点击时间发生的adapter,position是点击的位置,即item的位置
Person p=(Person) parent.getItemAtPosition(position);//因为getItemAtPosition是通过调用getItem来获得
//object,因为在MyAdapter中getItem返回的是person,所以通过
//调用这个方法返回的是person
Toast.makeText(getApplicationContext(), p.getId()+"", 0).show();
}
});
}
//自定义adapter,继承baseadapter
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();//获得adapter中item的数量
}
@Override
public Object getItem(int position) {
return persons.get(position);//获得位于position位置的item
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return persons.get(position).getId();///获得位于position位置的item的ID
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person p = persons.get(position);//获得位置为position的person
//创建View 用xml填充View并返回
View view = View.inflate(getApplicationContext(), R.layout.item,
null);
TextView tv1 = (TextView) view.findViewById(R.id.personid);// 如果直接通过findViewById查询查询的并不是View下的,而是mainactivity下的
TextView tv2 = (TextView) view.findViewById(R.id.name);
TextView tv3 = (TextView) view.findViewById(R.id.balance);
// 为view内的三个控件赋值
tv1.setText(p.getId().toString());
tv2.setText(p.getName().toString());
tv3.setText(p.getBalance().toString());
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第二种:直接使用simpleadapter
package com.example.sqllite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class SimpleAdapterActivity extends Activity {
List<Person> list;
ListView view;
List<HashMap<String, Object>> personList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDAO dao = new PersonDAO(getApplicationContext());
list = dao.queryAll();
view = (ListView) this.findViewById(R.id.list);
//list中存放的是map 而map中每个key是属性名称,value是person中的属性值
personList = new ArrayList<HashMap<String, Object>>();
//循环遍历list,将每一个person加入到map中
for (Person p : list) {
HashMap<String,Object> map=new HashMap<String, Object>();
map.put("ida", p.getId());
map.put("namea", p.getName());
map.put("balancea", p.getBalance());
personList.add(map);
}
//为ListView添加adapter,simpleadapter中第二个参数是List<HashMap<String, Object>>
//new String[] { "ida", "namea", "balancea"}代表map中的键值,new int[] { R.id.personid, R.id.balance, R.id.name }
//代表R.layout.item,中的textView的id
view.setAdapter(new SimpleAdapter(getApplicationContext(), personList,
R.layout.item, new String[] { "ida", "namea", "balancea" },
new int[] { R.id.personid, R.id.balance, R.id.name }));
view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String,Object> a=(HashMap<String, Object>) parent.getItemAtPosition(position);//因为在simpleadapter中放入的是List<HashMap<String, Object>>,所以返回的是hashmap
Toast.makeText(getApplicationContext(), (String)a.get("namea"),0).show();
}
});
}
}
第三种:直接使用simplecursoradapter
package com.example.sqllite;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class SimpleCursorAdapterActivity extends Activity {
ListView listView;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.list);
PersonDAO dao = new PersonDAO(getApplicationContext());
c = dao.queryCursor();
// 此处查询时id必须为_id
listView.setAdapter(new SimpleCursorAdapter(getApplicationContext(),
R.layout.item, c, new String[] { "_id", "name", "balance" },
new int[] { R.id.personid, R.id.name, R.id.balance }));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor c = (Cursor) parent.getItemAtPosition(position);// 传入的是Cursor所以返回的是Cursor ,源码// return (adapter== null || position < //0)?null : adapter.getItem(position);
String name = null;
if (c.moveToNext()) {
name = c.getString(c.getColumnIndex("name"));
}
Toast.makeText(getApplicationContext(), name, 0).show();
}
});
}
}