Search code examples
androidandroid-cursoradapterandroid-cursor

calculate value based on current and previous row in cursor android


I have a recycler adapter modified to use cursors set up and i am successfully returning results to a recyclerview via loader, etc. I am trying to display a calculation of a difference between the current record (displayed in the viewholder) and the previous record (may or may not be displayed on the UI based on the position).

The query that currently fills the adapter displays transaction amounts by month.

Example. If the user selects a sort by month they would get the following. (They can also sort by spending amount.)

month   |  spending
--------+----------
12/2014 | 123
--------|----------
11/2014 | 145
--------|----------
10/2014 | 130

What is the best practice to display the following as the user scrolls through the recyclerviews. I would like the user to be able to select the type of sort and based on the results find the difference from the previous record.

month   |  spending | difference
--------+-----------+------------
12/2014 | 123       | + 22
--------|-----------+------------
11/2014 | 145       | + 15
--------|-----------+------------
10/2014 | 130       | null

The reason i want to use a cursor is because i only want to load results based on the position of the cursor. Is there a way i can rewrite the query so i can get the results i want without doing the calculation when the data is presented in the adapter?

Option that currently works for me but i do not want to use:

I have have gotten the results i want by using an using an array list and doing the calculations during the creation of an array list but im afraid as the user enters more data (ive tested with 1000 dummy records), eventually the user will have to wait for the list to be created and loaded to UI every time the data set changes.

Please help me understand the best practice when it comes to these scenarios.


Solution

  • Ok, i thought of posting an answer of what i was telling you in the comments. ope this helps you somehow.

    • assuming you have a layout file custom_row.xml to represent each item in the list
    • and in this layout you have 3 textviews, say txtDate, txtSpend and txtDif:
    • your data list is an ArrayList<SpendingItem> where SpendingItem have (Date, Spend), and in the adapter class it's called items

    then getView will look like:

    @override
    public View getView(int position, View convertView, ViewGroup parent){
            //convertView == null or not, init ViewHolder, setTag or getTag (in case you are usin this approach to recycle views).
    
            //here comes the calculations:
            holder.txtDate.setText(items.get(position).getDate());
            holder.txtSpend.setText(items.get(position).getSpend() + "");//as getSpend() return int
            if(items.size() > position+1){
                holder.txtDif.setText((items.get(position+1).getSpend() - items.get(position).getSpend()) + "");
            //result is int, to avoid considering this int as a resource int (in strings xml file) or you can use Integer.toString()
            }else{
                holder.txtDif.setText("N/A");
            }
    
            // some other code ...
    
            return convertView;
    }
    

    EDIT: using CusrsorAdapter:

    @Override
    public void bindView(View arg0, Context context, Cursor cursor) {
        //here comes the calculations:
        holder.txtDate.setText(cursor.getString(1));
        holder.txtSpend.setText(cursor.getInt(2) + "");
        final int currentSpen = cursor.getInt(2); // conside index 2 is spending value in cursor
        if(cursor.moveToNext()){
            holder.txtDif.setText((cursor.getInt(2) - currentSpen) + "");
            cursor.moveToPrevious();//return to current position so you will not skip a row from cursor
        }else{
            holder.txtDif.setText("N/A");
        }
    
        // some other code ...
    }