RecyclerView not updating after calling changeCursor

我怕爱的太早我们不能终老 提交于 2020-08-10 13:12:10

问题


Background:

I have an orders table in my database whose data I show in RecyclerView. I do it by using a CursorAdapter in RecyclerView.Adapter. All of this is done in parent activity.

Besides this I have a child activity which is responsible for adding new orders to orders table and then coming back to parent activity to show all the orders in RecyclerView including the newly ones just created. New orders are added by starting a child activity in addOrder().

Problem:

When I come back from child activity to parent activity (after successful adding new orders to orders table) I try to refresh the RecyclerView with the help of changeCursor() but I don't see the RecyclerView getting updated with new data.

My Code:

Adapter:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {

    private CursorAdapter userList;
    private Context context;

    public OrderListAdapter(Context context, Cursor list){

        this.context = context;
        userList = new CursorAdapter(this.context, list, 0) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                // Code for inflater
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                // Code for bindView
            }
        };
    }

    public CursorAdapter getCursorAdapter(){ return userList; }

    public double getTotalPrice(){
        Cursor records = userList.getCursor();
        double total = 0;
        while (records.moveToNext()) {
              double price = records.getDouble(4);
              int quantity = records.getInt(3);
              total += price * quantity;
        }
        return total;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = userList.newView(context, userList.getCursor(), parent);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        userList.getCursor().moveToPosition(position);
        userList.bindView(holder.itemView, context, userList.getCursor());
    }

    @Override
    public int getItemCount() { return userList.getCount(); }

    public class ViewHolder extends RecyclerView.ViewHolder {
        // declaring holder variables
        public ViewHolder(View itemView){
            super(itemView);
           // Initialising holder variables
        }
    }
}

ParentActivity:

public class ParentActivity extends AppCompatActivity {

     private TextView totalText;
     private OrderListAdapter orderAdapter;
     private RecyclerView orderView; 

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_parent);
         totalText = findViewById(R.id.orderTotal);
         showTable();
     }

     private void setAdapter(Cursor records){
         orderView = findViewById(R.id.tableContent); 
         orderView.setLayoutManager(new LinearLayoutManager(this));
         orderList = new ArrayList<>();
         orderAdapter = new OrderListAdapter(this, records);
         orderView.setAdapter(orderAdapter);
     }

     private void showTotalPrice(){
         double totalPrice = orderAdapter.getTotalPrice();
         totalText.setText(String.valueOf(totalPrice));
     }

     private void showTable(String item){
         totalText.setText("0.0");
         Cursor records = db.query("orders", null, null, null, null, null, null);
         setAdapter(records);
         showTotalPrice();        
     }

     private void updateTable(){
         Cursor records = db.query("orders", null, null, null, null, null, null);
         orderAdapter.getCursorAdapter().changeCursor(records);
     }
     // This is called on click of a button 
     public void addOrder(View view){
         startActivity(new Intent(getApplicationContext(), ChildActivity.class));
         updateTable(); // this doesn't update the RecyclerView
         showTotalPrice();
     }
}

If I comment out showTotalPrice() in addOrder() the RecyclerView seems to get updated with new orders. Why is that the case ? why is showTotalPrice() preventing the update ? I need showTotalPrice() in order to show the updated total price of the products in the table.

来源:https://stackoverflow.com/questions/63271696/recyclerview-not-updating-after-calling-changecursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!