views:

3310

answers:

3

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;
 }
A: 

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.

cursor.moveToPosition(position);
Chiwai Chan
It still return one item! I Think it not work here.
Dennie
+3  A: 

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.

Dimitar Dimitrov
Hi, I try to edit my code with your advise. I use newView(), bindView() functions, and It return all items.But I still have a problem in my Listview, It display not really normal. I mean, It display different when I drag the scollbar up and down. May I forgot something?
Dennie
What do you mean by the items don't display normally? If you see old items showing up, instead of new ones (happened to me with normal adapter), there should be a problem with your recycling logic (bindView()).P.S.I would also recommend you to use the ViewHolder pattern, as explained here: http://www.youtube.com/watch?v=N6YdwzAvwOA at 0:09:02.
Dimitar Dimitrov
That video link is VERY helpful for anyone who wants to understand what's going on underneath the ListView. Thanks Dimitar!
Scienceprodigy
A: 

Question: What does your getCursor() method look like?

Aaron