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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…