如何从RecyclerView获取最新的最新价值

时间:2016-09-08 09:21:06

标签: android sqlite android-recyclerview android-cardview

我正在制作Garage应用程序,就像计算器一样。从EditText获取2个值并在第三个EditText中打印。然后它会将所有值存储在RecyclerView中。现在问题是我正在尝试获取最新的总毛值,即最后一行总价值我已经做了所有可能解决方案仍然没有得到。

以下是我在自定义适配器中的代码:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Employee employee = bank_details.get(position);


    holder.total.setText(employee.getTotal());
    holder.accept.setText(employee.getAcceptmoney());
    holder.given.setText(employee.getGivemoney());
    holder.note.setText(employee.getNote());
    holder.date.setText(employee.getTime());

    int selectedItem = 0;
    if(selectedItem == position)
        holder.itemView.setSelected(true);


}

@Override
public int getItemCount() {
    return bank_details.size();
}

public void setSelectedItem(int last_pos) {
}


public class MyViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView total;
    TextView accept;
    TextView given;
    TextView note;
    TextView date;

    Button edit;
    Button delete;

    public MyViewHolder(View itemView) {
        super(itemView);
        cv= (CardView) itemView.findViewById(R.id.cv);
        total = (TextView) itemView.findViewById(R.id.grossIncome);
        accept = (TextView) itemView.findViewById(R.id.acceptmoney);
        given = (TextView) itemView.findViewById(R.id.givenmoney);
        note = (TextView) itemView.findViewById(R.id.note);
        date= (TextView) itemView.findViewById(R.id.date);


    }
}
public Employee getItem(int position) {
    return bank_details.get(position);
}

以下是recyclerview的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recyclerview);

    final RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setHasFixedSize(true);

    final ArrayList<Employee> arr = InitializeData();

    final LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);

    cu = new CustomAdapter(arr);
    rv.setAdapter(cu);

    int last_pos = cu.getItemCount() - 1;
    cu.notifyDataSetChanged();
    cu.setSelectedItem(last_pos);
    rv.scrollToPosition(last_pos);
}

public ArrayList<Employee> InitializeData() {
    ArrayList<Employee> arr_emp = new ArrayList<>();
    ArrayList<Employee> arr_emp_2 = new ArrayList<>();



    DAL dal = new DAL(this);
    dal.OpenDB();
    arr_emp = dal.AllSelectQryForTabEmpData();
    dal.CloseDB();
    return arr_emp;



}
public void setSelectedItem(int position)
{
    selectedItem = position;
}

问题是我想要总价值,而且总价值可以用作给予和接收价值ie- 总计+收货金总给钱

1 个答案:

答案 0 :(得分:1)

您需要相应地更改适配器以添加新的最后一行。 为此,改变一些方法。

1)项目计数必须为+1

@Override
public int getItemCount() {
    return bank_details.size() + 1;
}

2)显示最后一行的最后一个值

向适配器说明为recyclerview的最后一行显示的内容。 当然,您必须通过浏览computeTotalValues()变量来对bank_details进行编码。

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    if (position >= bank_details.size() {

        double totalValue = computeTotalValue();
        //TODO update your holder views to show the total value.

    } else {
        Employee employee = bank_details.get(position);

        holder.total.setText(employee.getTotal());
        holder.accept.setText(employee.getAcceptmoney());
        holder.given.setText(employee.getGivemoney());
        holder.note.setText(employee.getNote());
        holder.date.setText(employee.getTime());

        int selectedItem = 0;
        if(selectedItem == position)
            holder.itemView.setSelected(true);

    }

}

3)如果数据已更改,请刷新最后一行

如果您在显示recyclerview时更改了bank_details信息(设置值,例如添加值),则需要警告适配器必须更新最后一行,方法是使用{{ 1}}(See docs)。