【发布时间】:2012-03-06 14:27:50
【问题描述】:
我在 alertDialog 中有一个列表视图。我使用自定义 resourceCursorAdapter 填充我的列表视图,每行包含一个标题 textView、日期 textView 和图像。
一切正常,除了看起来当您滚动列表视图时,某些行中的一些日期文本视图消失了。我已经弄清楚为什么它们消失了,只是不知道如何解决它......有些项目并不意味着显示日期,所以我在 bindview 上有一个 if 语句来确定是否应该显示日期。这适用于警报对话框之外的任何列表,但它会使一些本应显示的日期消失。有谁知道为什么会这样?
我的警报对话框和列表视图代码:
itemSendPickerDialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Item to Send");
ListView lv = new ListView(this);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
}
});
AlertDialog sendOptionAlert = sendOptionBuilder.create();
sendOptionAlert.show();
System.out.println("ID is: " + iid + " and position is: " + position);
itemSendPickerDialog.dismiss();
}
});
Cursor c = mDbHelper.fetchItemsByDate(id);
c.moveToFirst();
int i = R.layout.send_item_menu_row;
MyListAdapter ia = new MyListAdapter(this, i, c);
lv.setAdapter(ia);
builder.setView(lv);
itemSendPickerDialog = builder.create();
itemSendPickerDialog.show();
还有我的自定义 listAdapter:
class MyListAdapter extends ResourceCursorAdapter {
public MyListAdapter(Context context, int i, Cursor cursor) {
super(context, i, cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.item_name);
title.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TITLE)));
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
width = width - 150;
ViewGroup.LayoutParams params = title.getLayoutParams();
params.width = width;
title.setLayoutParams(params);
String cat = cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_CATEGORY));
if (cat.equalsIgnoreCase("trip notes")) {
LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder);
ll.setVisibility(View.INVISIBLE);
}
TextView date = (TextView) view.findViewById(R.id.item_date);
date.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_DATE)));
TextView time = (TextView) view.findViewById(R.id.item_time);
time.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_TIME)));
ImageView iv = (ImageView) view.findViewById(R.id.image_icon);
if (iv != null) {
int index = cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TYPE);
String type = cursor.getString(index);
if (type != null) {
} else {
type = "notes";
}
iv.setImageResource(getTypeResource(type));
}
}
public static int getTypeResource(String type) {
if (type.equalsIgnoreCase("flight")) {
return R.drawable.airplaneicon;
}
if (type.equalsIgnoreCase("boat/ship")) {
return R.drawable.boatshipicon;
}
if (type.equalsIgnoreCase("bus")) {
return R.drawable.busicon;
}
if (type.equalsIgnoreCase("rail")) {
return R.drawable.railicon;
}
if (type.equalsIgnoreCase("auto or other rentals")) {
return R.drawable.caricon;
}
if (type.equalsIgnoreCase("other transportation")) {
return R.drawable.othertransicon;
}
if (type.equalsIgnoreCase("hotel")) {
return R.drawable.hotelicon;
}
if (type.equalsIgnoreCase("resort")) {
return R.drawable.resorticon;
}
if (type.equalsIgnoreCase("bed & breakfast")) {
return R.drawable.bandbicon;
}
if (type.equalsIgnoreCase("camp ground")) {
return R.drawable.campgroundicon;
}
if (type.equalsIgnoreCase("vacation rental")) {
return R.drawable.vacationrentalicon;
}
if (type.equalsIgnoreCase("other lodging")) {
return R.drawable.otherlodgingicon;
}
if (type.equalsIgnoreCase("rail")) {
return R.drawable.railicon;
}
if (type.equalsIgnoreCase("cruise")) {
return R.drawable.cruiseicon;
}
if (type.equalsIgnoreCase("tour")) {
return R.drawable.touricon;
}
if (type.equalsIgnoreCase("dining")) {
return R.drawable.diningicon;
}
if (type.equalsIgnoreCase("spa")) {
return R.drawable.spaicon;
}
if (type.equalsIgnoreCase("class")) {
return R.drawable.classicon;
}
if (type.equalsIgnoreCase("other activity")) {
return R.drawable.eventicon;
} else {
return R.drawable.notesicon;
}
}
}
使文本消失的具体代码在bindview override中:
String cat = cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_CATEGORY));
if (cat.equalsIgnoreCase("trip notes")) {
LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder);
ll.setVisibility(View.INVISIBLE);
}
【问题讨论】:
标签: android eclipse android-listview textview android-alertdialog