简体   繁体   中英

Android custom cursor adapter

I have created a custom cursor adapter which fetches the value from the database and displays it in my defined view.

I have one textview and imageview. Depends on the text in the database image changes accordingly. I have defined MyArrayAdapter as follows, but it gives an error.

public class MyArrayAdapter extends SimpleCursorAdapter {

    private final Context context;
    private final Cursor cursor;
    private final String[] from;
    static class ViewHolder {
        public ImageView imageView;
        public TextView textView;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder;
        if (rowView == null) {
            LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.todo_row,null,true);
            holder = new ViewHolder();
            holder.textView = (TextView) rowView.findViewById(R.id.todo_edit_summary);
            holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
            rowView.setTag(holder);
        }else{
            holder = (ViewHolder) rowView.getTag();
        }

        String s = cursor.getString(cursor.getColumnIndex(from[0]));
        holder.textView.setText(s);

        if (cursor.getString(cursor.getColumnIndex("category")) == "Urgent"){
            holder.imageView.setImageResource(R.drawable.urgent);
        }else{
            holder.imageView.setImageResource(R.drawable.reminder);
        }

        return rowView;
    }
}

(Update from deleted answer)

The following code is working fine, but they are showing the same value in each and every entry in the ListView. I have extended SimpleCursorAdapter . Am I making any mistakes with managing the cursor?

public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder;
        if (rowView == null) {
            LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.todo_row,null,true);
            holder = new ViewHolder();
            holder.textView = (TextView) rowView.findViewById(R.id.label);
            holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
            rowView.setTag(holder);
        }else{
            holder = (ViewHolder) rowView.getTag();
        }

        String s = cursor.getString(cursor.getColumnIndex(from[0]));
        holder.textView.setText(s);

        if ((cursor.getString(cursor.getColumnIndex("category"))).equals("Urgent")){ 
            holder.imageView.setImageResource(R.drawable.reminder);
        }else{
            holder.imageView.setImageResource(R.drawable.urgent);
        }

        return rowView;
    } 

As far as I can tell the only part of SimpleCursorAdapter you are using is the from parameter which you can easily substitute with a literal string since you are extending it. While I'm not familiar with the "guts" of the code it's pretty safe to assume that since getView is not mentioned in SimpleCursorAdapter documentation that it's not meant to be overridden lightly since doing so can break it like in your example.

Based on what you have, I've adapted it to extend CursorAdapter instead and by splitting the code you had in getView into newView and bindView, both of which are defined as abstract methods in CursorAdapter.

public class MyCursorAdapter extends CursorAdapter {

    static class ViewHolder {
        public ImageView imageView;
        public TextView textView;
    }

    public MyArrayAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.todo_row,null,true);

        ViewHolder holder = new ViewHolder();
        holder.textView = (TextView) rowView.findViewById(R.id.todo_edit_summary);
        holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
        rowView.setTag(holder);

        return rowView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
       ViewHolder holder = (ViewHolder) view.getTag();

       String s = cursor.getString(cursor.getColumnIndex("whatever from[] represented"));
       holder.textView.setText(s);

       if (cursor.getString(cursor.getColumnIndex("category")) == "Urgent"){
           holder.imageView.setImageResource(R.drawable.urgent);
       }else{
           holder.imageView.setImageResource(R.drawable.reminder);
       }       
    }
}

To compare between value of two strings use equals() of String class. Donot use == operator in this case

So use the following code

if ((cursor.getString(cursor.getColumnIndex("category"))).equals("Urgent")){
        holder.imageView.setImageResource(R.drawable.urgent);
    }else{
        holder.imageView.setImageResource(R.drawable.reminder);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM