gpt4 book ai didi

android - 从android中的光标获取所有项目

转载 作者:搜寻专家 更新时间:2023-11-01 07:41:22 24 4
gpt4 key购买 nike

我使用这段代码从光标中获取项目,但它只返回我列表中的一项。那么,我怎样才能将所有项目都添加到我的列表中,这是我的代码?

class MyAdapter extends SimpleCursorAdapter
{
private Context context;

public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;

}
public View getView(int position, View convertView, ViewGroup parent){
Cursor cursor = getCursor();

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View v = inflater.inflate(R.layout.sbooks_row, null);
TextView title = (TextView)findViewById(R.id.title);
if(title != null){
int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE);
String type = cursor.getString(index);
title.setText(type);
}

TextView lyrics = (TextView)findViewById(R.id.lyrics);
if(lyrics != null){
int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_LYRICS);
String type = cursor.getString(index);
lyrics.setText(type);
}

ImageView im = (ImageView)findViewById(R.id.icon);
if(im!=null){
int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_FAVORITE);
int type = cursor.getInt(index);
if(type==1){
im.setImageResource(android.R.drawable.btn_star_big_on);
}
else{
im.setImageResource(android.R.drawable.btn_star_big_off);
}
}

return v;
}

最佳答案

CursorAdapter 的行为与其他列表适配器略有不同 - 而不是在 getView() 中,这里的魔法发生在 newView() 和 bindView() 中,所以我认为 getView() 不是正确的覆盖方法。

您可能只会得到一个结果,因为在创建第一行后,CursorAdapter 期望 bindView() 插入新数据并重用已经膨胀的行,而您期望 getView() 这样做。

我建议您尝试将代码移至 newView() 以扩充 View ,并将代码移至 bindView() 以执行填充行的实际逻辑。

祝你好运,让我们了解最新的结果。

关于android - 从android中的光标获取所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1305272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com