Android 基于光标中的当前行和上一行计算值

Android 基于光标中的当前行和上一行计算值,android,android-cursoradapter,android-cursor,Android,Android Cursoradapter,Android Cursor,我修改了一个回收器适配器,以使用设置的游标,并通过加载器等将结果成功返回到recyclerview。我试图显示当前记录(显示在viewholder中)与上一记录(根据位置可能显示在UI上,也可能不显示在UI上)之间的差异计算 当前填充适配器的查询按月显示交易金额 例如。如果用户选择按月排序,他们将得到以下结果。(他们也可以按支出金额排序。) 当用户在recyclerviews中滚动时显示以下内容的最佳做法是什么。我希望用户能够选择排序的类型,并根据结果找到与以前记录的差异 month |

我修改了一个回收器适配器,以使用设置的游标,并通过加载器等将结果成功返回到recyclerview。我试图显示当前记录(显示在viewholder中)与上一记录(根据位置可能显示在UI上,也可能不显示在UI上)之间的差异计算

当前填充适配器的查询按月显示交易金额

例如。如果用户选择按月排序,他们将得到以下结果。(他们也可以按支出金额排序。)

当用户在recyclerviews中滚动时显示以下内容的最佳做法是什么。我希望用户能够选择排序的类型,并根据结果找到与以前记录的差异

month   |  spending | difference
--------+-----------+------------
12/2014 | 123       | + 22
--------|-----------+------------
11/2014 | 145       | + 15
--------|-----------+------------
10/2014 | 130       | null
我想使用光标的原因是,我只想根据光标的位置加载结果。是否有一种方法可以重写查询,以便在适配器中显示数据时,无需进行计算即可获得所需的结果

当前适用于我但我不想使用的选项:

通过使用数组列表并在创建数组列表的过程中进行计算,我已经得到了我想要的结果,但我担心随着用户输入更多数据(我用1000条虚拟记录进行了测试),最终,每次数据集更改时,用户必须等待创建列表并加载到UI


请帮助我了解这些场景的最佳实践。

好的,我想在评论中对我告诉你的内容给出一个答案。 这对你有帮助

  • 假设您有一个布局文件
    custom_row.xml
    来表示列表中的每个项目
  • 在这个布局中,您有3个文本视图,例如txtDate、txtplown和txtDif:
  • 您的数据列表是一个
    ArrayList
    列表,其中
    SpendingItem
    具有(日期、支出),在适配器类中称为
    items
然后getView将看起来像:

@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;
}
编辑: 使用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 ...
}

如果您不想一次计算全部内容(加载时间),那么您可以使用自定义适配器填充列表,然后您可以使用
getView()
中请求位置和位置-1(如果存在)处的项目计算dif,但在您的情况下,它是位置和位置+1[2014年12月旁边的diff是spendingOf(2014年11月)-spendingOf(12/2014)],对吗?我忘了提到我使用的回收器适配器修改为使用光标。是的,您的假设是正确的,但是如果用户向上滚动,则必须是位置+1,如果用户向下滚动,则必须是位置-1,因为顶部的视图在从UI中消失时会被回收。因此,我认为必须有更好的方法。例如,重写查询以提供所需的结果,这样我就不必在适配器中进行计算,因为视图是循环的。不,您不必检测滚动的方向,它应该是相同的,为了获得相同的结果,这是正确的结果,无论滚动的方向如何,尝试在getView()中执行别忘了检查(位置+1)是否超出索引(data.size()>(位置+1))…好的,在重新思考您所说的内容后,这是有意义的。这只是位置+1,但是如果在onBindViewHolder期间使用数组列表,那么您所说的很容易做到。我不确定如何使用光标来执行此操作。谢谢您的帮助。我在最初的帖子中写道,我有一个有效的解决方案,正如你在上面所建议的那样。我正在通过光标请求解决方案。因此,我不必每次数据库发生更改时都将整个查询结果重新加载到arraylist中,因为当有500多条记录时,加载适配器需要很长时间。@user3300856抱歉,不理解这种方式,请检查更新的答案,添加了一个使用自定义cusrsoradapter的建议sol谢谢我会尝试,希望当用户快速滚动时不会出现性能问题,然后我会回复
@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 ...
}