更新了答案 我有一个listview,它使用SimpleCursorAdapter从Sqlite数据库获取数据。为此,我创建了一个自定义的TodoCursorAdapter类。
我使用游标从数据库中获取3个字段值,并使用此适配器将这些值放入3个textviews中,我想从数据库中获取另一个字段callType
,并根据此字段的值,我想在imageview中显示特定图像。
如果callType
值为outgoing
,我想显示图片;如果callType
值为incoming
,则显示另一张图片。我尝试在bindView中使用if else但是没有工作。你能指出我在这里缺少的东西吗?
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();
}
}
答案 0 :(得分:0)
最后我开始工作了。 Thanx到pskink。我重写了setViewImage方法,并根据值设置条件来检查值并设置图像。
Paragraph p = new Paragraph("This too shall pass");
p.Alignment = Element.ALIGN_CENTER;