Get all element from cursor in android

I am using this code to get an item from the cursor, but it just returns one item in my list. So how can I get all the items in my list, is this 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;
    }

      

+2


source to share


2 answers


CursorAdapter behaves slightly differently than other list adapters. Instead of getView (), the magic happens here in newView () and bindView (), so I think getView () is not suitable to be overridden.

You can only get one result, because after creating the first row, CursorAdapter expects bindView () to insert new data and reuse the already inflated row, and you expect getView () to do this.



I suggest you try moving your code to newView () to inflate your view, and bindView () to do the actual logic to populate the rows.

Good luck and will keep us updated on the results.

+5


source


My guess is that the cursor returned by the getCursor () method is fetching all the rows for your table correctly, you must explicitly move the cursor to the position before the data access for the row, so at the start of your getView () you must call.



cursor.moveToPosition(position);

      

0


source







All Articles