Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

android - Change image according to value in a database field in SimpleCursorAdapter's bindView?

Updated Answer I have a listview which is getting data from Sqlite Database using SimpleCursorAdapter. For this i created a custom TodoCursorAdapter class.

I am getting 3 field values from database using cursor and putting these values into 3 textviews using this Adapter and I want to get another field callType from Database, and, according to this field's value, I want to show a particular image in an imageview.

I want to show an image if callType value is outgoing and show another image if callType value is incoming. I tried to use if else in bindView but not working. Can you please point what i am missing here?

TodoCursorAdapter.java:

public class TodoCursorAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;

public TodoCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.layout=layout;
    this.mContext = context;
    this.inflater=LayoutInflater.from(context);
    this.cr=c;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return inflater.inflate(layout, viewGroup, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);

    view=inflater.inflate(layout, null, false);
    TextView tvNumber = (TextView) view.findViewById(R.id.tvNumber);
    TextView tvDuration = (TextView) view.findViewById(R.id.tvDuration);
    TextView tvDateTime = (TextView) view.findViewById(R.id.tvDateTime);
    ImageView img2 = (ImageView) view.findViewById(R.id.img2);

    String number = cursor.getString(cursor.getColumnIndexOrThrow("displayname"));
    String duration = String.valueOf(cursor.getInt(cursor.getColumnIndexOrThrow("fileduration")));
    String dateTime = cursor.getString(cursor.getColumnIndexOrThrow("filedatetime"));
    String type = cursor.getString(cursor.getColumnIndexOrThrow("calltype"));

    tvNumber.setText(number);
    tvDuration.setText(duration);
    tvDateTime.setText(dateTime);

    if (type.equals("incoming")){
        img2.setImageResource(R.drawable.incomingcall);
    }else {
        img2.setImageResource(R.drawable.outgoing);
    }

}

MainActivity.java

    lv = (ListView) findViewById(R.id.lv);
    db = new DBSQL(getApplicationContext());
    populateListview();

public void populateListview(){
    db = new DBSQL(MainActivity.this);
    cursor = db.getAllData();
    String[] from = new String[]{"displayname", "fileduration", "filedatetime"};
    int[] to = new int[]{R.id.tvNumber, R.id.tvDuration, R.id.tvDateTime};
    if (adapter == null){
        adapter = new TodoCursorAdapter(MainActivity.this, R.layout.custom_listview, cursor, from, to);
        lv.setAdapter(adapter);

    }else {
        adapter.changeCursor(cursor);
    }


}

@Override
public void onResume() {
    populateListview();
    super.onResume();
}


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally I got it working. Thanx to pskink. I overrided setViewImage method and put a condition to check value and set image according to value.

@Override
public void setViewImage(ImageView v, String value) {
    if (value.equals("incoming")){
        v.setImageResource(R.drawable.incomingcall);
    }else {
        v.setImageResource(R.drawable.outgoing);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...