在我的应用程序中有一个listview,其中我必须显示4个值,每个列表项都有一个复选框...值来自数据库..问题是数据库中的值没有被分配给listitem文本视图..;
这是我指定的活动
private List<PaymentDueModel> getModel() {
List<PaymentDueModel> list = new ArrayList<PaymentDueModel>();
try {
Cursor cursor = database.fetchCursor("member_dues", new String[] {
"INST_DUE_DATE", "PDI_AMOUNT", "TXNTYPE" },
"MEMBERSHIP_ID = '" + MEMBERSHIP_ID + "'", null, null,
null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
list.add(get(cursor.getString(0), cursor
.getString(1), cursor.getString(2)));
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
list = null;
}
Log.e("list",list.toString());
return list;
}
private PaymentDueModel get(String n,String t, String s) {
return new PaymentDueModel(n,t,s);
}
这是适配器:
public class PendingListAdapter extends ArrayAdapter<PaymentDueModel> {
private final List<PaymentDueModel> list;
private final Activity context;
public PaymentDueModel element;
public PendingListAdapter(Activity context, List<PaymentDueModel> list) {
super(context, R.layout.list_item_pending, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
TextView dueDate;
TextView amount;
TextView paymentType;
CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
try
{
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.list_item_pending, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.dueDate =(TextView)view.findViewById(R.id.txt_due_date);
viewHolder.amount=(TextView)view.findViewById(R.id.txt_amount);
viewHolder.paymentType=(TextView)view.findViewById(R.id.txt_payment_type);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check_Status);
// viewHolder.checkbox
// .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//
// @Override
// public void onCheckedChanged(CompoundButton buttonView,
// boolean isChecked) {
// PaymentDueModel element = (PaymentDueModel) viewHolder.checkbox
// .getTag();
//
// }
// });
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(list.get(position).isSelected());
holder.dueDate.setTag(list.get(position).getdueDate());
holder.amount.setTag(list.get(position).getamount());
holder.paymentType.setTag(list.get(position).getpaymentType());
}
catch(Exception e)
{
e.printStackTrace();
}
return view;
}
}
这是getter setter类:
public class PaymentDueModel {
private String dueDate;
private String amount;
private String paymentType;
private boolean selected;
public PaymentDueModel(String dueDate, String amount, String paymentType) {
this.dueDate = dueDate;
this.amount = amount;
this.paymentType = paymentType;
selected = false;
}
public String getdueDate() {
return dueDate;
}
// public void setdueDate(String dueDate) {
// this.dueDate = dueDate;
// }
public String getamount() {
return amount;
}
// public void setamount(String amount) {
// this.amount = amount;
// }
public String getpaymentType() {
return paymentType;
}
// public void setpaymentType(String paymentType) {
// this.paymentType = paymentType;
// }
public boolean isSelected() {
return selected;
}
public void setselected(boolean selected) {
this.selected = selected;
}
}
适配器设置在onCreate中。 我在这里设置适配器..
pending_adapter = new PendingListAdapter(getActivity(), getModel());
member_pending_list.setAdapter(pending_adapter);
请帮忙......我无法弄清楚问题..
答案 0 :(得分:4)
而不是setTag
holder.dueDate.setTag(list.get(position).getdueDate());
holder.amount.setTag(list.get(position).getamount());
holder.paymentType.setTag(list.get(position).getpaymentType());
使用setText
holder.dueDate.setText(list.get(position).getdueDate());
holder.amount.setText(list.get(position).getamount());
holder.paymentType.setText(list.get(position).getpaymentType());