Android 如何从SQLite加载带有列表的自定义Listview
我创建了Adapter类,它扩展了BaseAdapter,我还创建了用于查询DB的类。现在我很擅长连接结果和自定义布局,我只是不知道如何实现它。如果有人能帮忙的话 适配器类Android 如何从SQLite加载带有列表的自定义Listview,android,sqlite,listview,adapter,Android,Sqlite,Listview,Adapter,我创建了Adapter类,它扩展了BaseAdapter,我还创建了用于查询DB的类。现在我很擅长连接结果和自定义布局,我只是不知道如何实现它。如果有人能帮忙的话 适配器类 public class DrugAdapter extends BaseAdapter{ private static LayoutInflater lf = null; private Activity activity; private ArrayList<HashMap<Strin
public class DrugAdapter extends BaseAdapter{
private static LayoutInflater lf = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
public DrugAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
HashMap<String, String> drug = new HashMap<String, String>();
drug = data.get(position);
// Setting all values in list view
//title.setText());
//description.setText(song.get(CustomizedListView.KEY_ARTIST));
return vi;
}
}
请知道我应该如何设置ListView适配器
我试过一些像这样的把戏
List k =db.getAllDrugs();
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
ls.setAdapter(adap);
最后一个setText正在标记错误。您需要将包含数据库数据的列表传递给适配器。然后使用
getView()方法中列表中的数据。我想你可以去掉HashMap 首先替换列表k=db.getAllDruges()
byList k=db.getAllDruges()代码>
然后,编译器会抱怨适配器是字符串适配器。此时,请更换
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
编译器现在会抱怨列表的类型不正确。将DrugAdapter
中对ArrayList数据的引用更改为List data
然后正确执行getItem
:
public Drug getItem(int position) {
return data.get(position);
}
在getView
declaredrug
中,如下所示:
Drug drug = data.get(position);
然后尽一切可能将druge
的内容放入视图中。您的自定义适配器通常不需要从BaseAdapter
继承。该框架中有许多适配器实现,它们负责一系列样板文件。在您的情况下,出于性能原因,您应该使用游标适配器。游标针对平台进行了优化,将其转储到阵列对于任何非微小的结果都是浪费
public class DrugAdapter extends CursorAdapter {
public DrugAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public Object getItem(int position) {
// This allows us to get proper Drug objects from the adapter
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
return drug;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// We only need to create the new view here
final LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.single_list_item, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// This is where we populate the list item data
((TextView) view.findViewById(R.id.drug_title)).setText(cursor.getString(1));
((TextView) view.findViewById(R.id.drug_description)).setText(cursor.getString(2));
}
}
你应该多学习一下ArrayList
,你缺少了一些非常简单的东西
必须为列表视图设置适配器,如下所示:
myListView.setAdapter(new DrugAdapter(getActivity(), getAllDrugs()));
public class DrugAdapter extends BaseAdapter{
private LayoutInflater lf = null;
private Activity activity;
private ArrayList<Drug> dl;
public DrugAdapter(Activity a, ArrayList<Drug> drugList){
activity = a;
dl = drugList;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dl.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
title.setText(dl.get(position).getName());
//use dl.get(position).yourMethodName() to get its value accordingly to your needs
}
}
此外,DrugAdapter类的第二个参数是错误的,如果GetAllDruges()
返回List
对象,则DrugAdapter构造函数必须如下所示:
public DrugAdapter(Activity a, ArrayList<Drug> drugList);
请记住:这段代码没有性能增强,我没有增强它,因为它不是您所要求的,请尝试找出以后可以做些什么来提高它的性能,但首先,试着了解一下ArrayList
你应该像@Raghunandan的工作一样发布stacktraceounds什么堆栈Trace@1baga但他们一直在破坏应用程序。撞车对。查看logcat并发布异常部分here@Raghunandan这是我做的尝试,看看它是否工作List k=db.getAllDruges();ArrayAdapter adap=新的ArrayAdapter(这是android.R.layout.simple_list_item_1,k);ls.setAdapter(adap)
如果我要使用您提供的这个类,那么当我在Mainactivity类中使用对象时,我应该传递给对象的参数是什么?当您第一次创建适配器时,上下文
应该是当前的活动
,而光标
应该为空。查询完成后(应该在主线程之外执行),在适配器上调用swapCursor()
,然后关闭旧的光标(如果它不是null)。只是好奇。。那么这个类如何知道要处理什么列表呢..因为它是..它没有接收任何要处理的数据..您仍然负责执行数据库查询(就像在您的getAllDruges()
方法中一样),但不要将其转储到列表中,只需通过swapCursor()将原始光标传递给适配器即可
@njzk2抱歉打扰您。据我所知,我一直在处理此错误。一切正常,但当我在自定义布局中添加新视图并尝试将药物内容放入新视图时。它会不断使应用程序崩溃,但每当我删除它并放入随机文本时,它就会工作<代码>//设置列表视图title.setText(drug.getName())中的所有值;description.setText(drug.get_desc());id.setText(“h”)代码>@1baga:你必须发布一个新的问题,而不是在这样调试的地方发表评论(这只是因为它不太方便,也没有太多的空间或格式化功能。)@1baga:这个问题与原来的问题不同。请另发一个问题。当你这样做的时候,张贴stacktrace。
public class DrugAdapter extends CursorAdapter {
public DrugAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public Object getItem(int position) {
// This allows us to get proper Drug objects from the adapter
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
return drug;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// We only need to create the new view here
final LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.single_list_item, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// This is where we populate the list item data
((TextView) view.findViewById(R.id.drug_title)).setText(cursor.getString(1));
((TextView) view.findViewById(R.id.drug_description)).setText(cursor.getString(2));
}
}
myListView.setAdapter(new DrugAdapter(getActivity(), getAllDrugs()));
public DrugAdapter(Activity a, ArrayList<Drug> drugList);
public class DrugAdapter extends BaseAdapter{
private LayoutInflater lf = null;
private Activity activity;
private ArrayList<Drug> dl;
public DrugAdapter(Activity a, ArrayList<Drug> drugList){
activity = a;
dl = drugList;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dl.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
title.setText(dl.get(position).getName());
//use dl.get(position).yourMethodName() to get its value accordingly to your needs
}
}