3

私の各行にバインドされたデータベース値の小計を表示しようとしていますListView。小計をデータベースの列として保持したくないので、表示される値を累積しようとしています。もちろん、これはスクロールを処理する方法の明らかな問題を引き起こします。スクロールは値を表示および再表示し、実行中の小計を台無しにします。

詳しく説明すると、リストビューの各行には、値と小計の 2 つのフィールドがあります。値はデータベース行から取得され、小計はその行までのすべてのデータベース行の合計です。行の挿入と削除がより困難になるため、必要がない場合は、小計をデータベース行に含めたくありません。

何か案は?

4

2 に答える 2

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);
    }
}
于 2012-04-27T06:44:43.303 に答える
0

ListView に読み込まれている値を反復処理し、同時に計算します...

于 2012-04-27T03:18:45.537 に答える