在android中获取光标处的所有项目
我使用此代码从光标获取项目,但它只返回列表中的一项。那么,如何将所有项目添加到我的列表中,这是我的代码?
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;
}
I use this code to get the item from cursor, but it just return one item on my list. So, how can I get all items to my list, this is my code?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CursorAdapter 的行为与其他列表适配器有点不同 - 这里的魔力不是在 getView() 中,而是在 newView() 和 bindView() 中,所以我认为 getView() 不是重写的正确方法。
您可能只会得到一个结果,因为创建第一行后,CursorAdapter 期望 bindView() 插入新数据并重用已经膨胀的行,并且您期望 getView() 执行此操作。
我建议您尝试将代码移至 newView() 来扩充视图,并移至 bindView() 来执行填充行的实际逻辑。
祝你好运,请随时向我们通报最新结果。
CursorAdapter behaves a little bit different than the other list adapters - instead of in getView(), the magic here happens in newView() and bindView(), so I think getView() isn't the right method to override.
You may get only one result, because after the first row is created, CursorAdapter expects bindView() to insert new data and reuse the already inflated row, and you expect getView() to do that.
I'll suggest you to try moving your code to newView() to inflate your view, and bindView() to execute the actual logic for populating the rows.
Good luck, and keep us updated on the results.
我假设 getCursor() 方法返回的光标正确检索表的所有行,您必须在访问行的数据之前显式地将光标移动到某个位置,因此在 getView() 方法的开头你必须打电话。
I am presuming the cursor returned by the getCursor() method is correctly retrieving all the rows for your table, you have to explicitly move the cursor to a position before accessing the data for a row, so at the beginning of your getView() method you have to call.