现在我正在尝试使用
打开数据库db=SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Cursor resultset = db.query("person_table", null, null, null, null, null, null);
所以我使用Cursor来获取db查询返回的每个结果集。每个结果集返回我添加到数组中以在ListView中查看。下面是我从db
获取数据的代码package com.android.my.mypackage;
import android.app.ListActivity;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.Cursor;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class personDetail extends ListActivity{
private static String DBPATH="data/data/com.android.my.mypackage/databases/";
private static String DBNAME="persondb.db";
private View refView;
public ListView personlist;
public personDetail(View v, ListView l){
refView = v;
personlist = l;
}
public void DisplayPersonDetail(){
SQLiteDatabase db=null;
String path=DBPATH+DBNAME;
db=SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Cursor resultset = db.query("person_table", null, null, null, null, null, null);
if(resultset.getCount()==0){
/*please ignore this block*/
}else{
resultset.moveToFirst();
List<String> arl = new ArrayList<String>();
while(resultset.isAfterLast()==false){
arl.add(resultset.getString(6).toString() + "@@" + resultset.getString(1).toString());
resultset.moveToNext();
}
//Toast.makeText(refView.getContext(), "Arl value " + arl, Toast.LENGTH_LONG).show();
personlist.setAdapter(new PersonAdapter(this, arl));
}
}
}
以下是PersonAdapter类
package com.android.my.mypackage;
import java.util.List;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ImageView;
public class PersonAdapter extends ArrayAdapter<String>{
private static Activity context;
private final List<String> list;
public PersonAdapter(Activity c, List<String> list){
super(context, R.layout.person_adapter, list);
this.context = c;
this.list = list;
}
static class ViewHolder{
private TextView descHolder;
private ImageView typeHolder;
}
public View getView(int position, View convertview, ViewGroup group){
ViewHolder holder;
View lView = convertview;
if(lView==null){
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lView = inf.inflate(R.layout.person_adapter, group, true);
holder = new ViewHolder();
holder.descHolder = (TextView) lView.findViewById(R.id.personDescLbl);
holder.typeHolder = (ImageView) lView.findViewById(R.id.personCategoryImage);
}else{
holder = (ViewHolder) lView.getTag();
}
holder.descHolder.setText(list.get(position).toString().split("@@")[0]);
String category = list.get(position).toString().split("@@")[1];
if(category.equals("Male")){
holder.typeHolder.setImageResource(R.drawable.male);
}else{
holder.typeHolder.setImageResource(R.drawable.female);
}
return lView;
}
}
和我的person_adapter布局用列表视图充气
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/personDescLbl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"/>
<ImageView
android:id="@+id/personCategoryImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
我的代码基于此site的教程。但我在PersonAdapter类中得到Nullpointer异常错误,该错误指向
super(context, R.layout.person_adapter, list);
在此之前,我使用表格布局显示结果集,并且它正在工作查找。但是,使用表格布局来显示大量数据并不是一个很好的实用方法,因为它可以节省时间和内存。所以请帮我完成listView适配器。感谢。
答案 0 :(得分:0)
在您的PersonAdapter
中,当您致电context
构造函数时,您的null
字段为super
。
您只在第二行初始化它。您应该将活动作为super
的第一个参数(您称之为c
),然后影响您的context
字段。
public class PersonAdapter extends ArrayAdapter<String>{
private static Activity context;
...
public PersonAdapter(Activity c, List<String> list){
super(c, R.layout.person_adapter, list);
this.context = c;
this.list = list;
}