我正在尝试显示绑定到ListView
每行的数据库值的小计。我不想将子总数保留为数据库列,因此我尝试在显示时累积值。当然,这会带来如何处理滚动的明显问题,滚动会显示并重新显示一个值,从而搞乱运行的子总数。
详细说明,每个listview行都有两个字段,一个值和一个小计。该值来自数据库行,小计是所有数据库行的总和,包括该行。如果不必要,我不想将子总数包含在数据库行中,因为这会使插入和删除行更加困难。
有什么想法吗?
答案 0 :(得分:2)
创建一个自定义适配器,创建一个数组来保存小计,并在getView中使用它来保持小计,即使滚动也是如此。
public class CustomCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private Activity activity;
private int[] subtotal;
private int subtotalhold;
private int layout;
public CustomCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
this.activity = (Activity) context;
this.layout = layout;
subtotal = new int[c.getCount()];
subtotalhold=0;
c.moveToFirst();
int i = 0;
while (c.isAfterLast() == false) {
subtotalhold = subtotalhold + c.getInt(columnIndex);
subtotal[i] = subtotalhold;
i++;
c.moveToNext();
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = View.inflate(context, layout, null);
c.moveToPosition(position);
TextView subtotal = (TextView) convertView.findViewById(R.id.subtotal);
subtotal.setText(subtotal[position]);
// rest of your code to populate the list row
return (row);
}
}
答案 1 :(得分:0)
迭代加载到ListView中的值,然后同时计算它......