Display sub-tables in each line of the list

I am trying to show the general value of a database attached to every row of mine ListView. I don't want the sub-total as a database column, so I'm trying to accumulate values ​​as they are displayed. Of course, this causes obvious problems with how to deal with scrolling, which will display and re-display the value, spoiling the current subtotal.

To clarify, each line of the list contains two fields, a value and a sub-total. The value comes from the database row, the total is the total of all database rows before and including this row. I do not want to include sub-total in the database row if I do not need this, because it makes it difficult to insert and delete rows.

Any ideas?

+3
source share
2 answers

Create a custom adapter, create an array to hold the subtotal, and use it in your getView to maintain your subtotal even when scrolling.

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);
    }
}
+2
source

Iterate through the values ​​that are loaded into the ListView and then compute them at the same time ...

0
source

All Articles