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;
}
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.