0

I am new in android. I am using content provider for my app and want to delete an item from my sqlite database. it deletes the item from the database but the recyclerview does not show that it has been deleted except you go back and open the activity again before the deleted item disappears. I have looked through the code but I dont actually know what the problem is.

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selecte_problem_drawal);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));


    mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    selectedProblemsAdapter = new SelectedProblemsAdapter(this, null);
    mRecyclerView.setAdapter(selectedProblemsAdapter);

    getLoaderManager().initLoader(SELECTED_PROBLEMS_LOADER_ID, null,new CartLoader());


    payForSelectedProblems = (Button)findViewById(R.id.payForSelectedProblems);
    payForSelectedProblems.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (total > 0) {
                Preferences.setDefaults("total", String.valueOf(total), getApplicationContext());
                Intent intent = new Intent(SelectedProblems.this, SecondPayment.class);
                startActivity(intent);
            }else{
                AlertDialog.Builder alert = new AlertDialog.Builder(SelectedProblems.this);
                alert.setTitle("Alert");
                alert.setMessage("Kindly Select a problem to continue...");
                alert.setPositiveButton("OK",null);
                alert.show();
            }

        }
    });
}
public class CartLoader implements LoaderManager.LoaderCallbacks<Cursor> {
    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {

        return new CursorLoader(
                SelectedProblems.this,
                TranxavContract.CartEntries.CONTENT_URI,
                SELECTED_PROBLEMS,
                null,
                null,
                null
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        selectedProblemsAdapter.update(cursor);
        total = getSelectedProblemTotal(cursor);
        problemSelectedTotal.setText(CurrencyFormatter.currencyFormat(String.valueOf(total)));
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader){
        selectedProblemsAdapter.update(null);
    }

    public double getSelectedProblemTotal(Cursor cursor){
        while (cursor.moveToNext()){
            total = total + Double.parseDouble(cursor.getString(SelectedProblems.PRICE));
        }
        return total;
    }

}

Adapter

  public SelectedProblemsAdapter(Context context, Cursor cursor) {
    this.context = context;
    this.cursor = cursor;
}

@NonNull
@Override
public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
    return new SelectedProblemsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
    if (this.cursor != null){
        this.cursor.moveToPosition(position);
        priceIndex = cursor.getColumnIndex(TranxavContract.CartEntries.PRICE);
        problemsIndex  = cursor.getColumnIndex(TranxavContract.CartEntries.PROBLEMS);


        id = cursor.getString(idIndex);
        problems = cursor.getString(problemsIndex);
        price = cursor.getString(priceIndex);
        formatted_price = CurrencyFormatter.currencyFormat(price);
        holder.selectedProblemPrice.setText(formatted_price);
        holder.selectedProblems.setText(problems);

        holder.remove_problem_from_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.getContentResolver().delete(
                        TranxavContract.CartEntries.CONTENT_URI,
                        TranxavContract.CartEntries.PROBLEMS + " = ?",
                        new String[] { problems }
                );
                Toast.makeText(context, problems + " Item Removed from Problem Selected" + "with postion " + position, Toast.LENGTH_SHORT).show();
                notifyItemRemoved(position);
                notifyDataSetChanged();
            }
        });
    }
}

@Override
public int getItemCount() {
    return (null != cursor ? cursor.getCount(): 0);
}

public void update(Cursor cursor) {
    this.cursor = cursor;
    notifyDataSetChanged();
}

class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{

    TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
    Button remove_problem_from_cart;
    public SelectedProblemsViewHolder(View itemView) {
        super(itemView);
        selectedProblems = itemView.findViewById(R.id.selected_problems);
        selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
        selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
        remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);

    }
}

Anybody who can help me.

4 Answers4

1

For every delete, update your adapter like this

selectedProblemsAdapter.notifydatasetchanged();
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Cong Fandi
  • 71
  • 7
0

Assuming all your deleting process is correct and you want to show animation all over your list in RecyclerView.

Instead of using notifyItemRemoved(position) you could implement

notifyItemRangeChanged(position, getItemCount());

notifyItemRangeChanged() notifies the RecyclerView Adapter that positions of element in adapter has been changed from position.

Here is a reference for you.

Hope it helps!

Angus
  • 3,680
  • 1
  • 12
  • 27
  • It animates the recyclerview but does not remove the item from the recyclerview – oshabz Chuks Aug 08 '18 at 02:12
  • @oshabzChuks If it animated the recyclerview which means it has call the function, referring to " does not remove the item", I guess you did not even perform the delete operation, have you check your sqlite database data? – Angus Aug 08 '18 at 02:14
  • Yes I have checked it with DB browser for sqlite the row is deleted from the database but shows on the recyclerview – oshabz Chuks Aug 08 '18 at 02:33
0

May be the problem is the list hasn't been reload after you deleted the record in db. You can try the method getLoaderManager().restartLoader(int id, Bundle args, LoaderCallbacks<D> callback) this should be restart the query and update it from the db. I hope so. Check more at here

YenMinh
  • 509
  • 5
  • 12
0

After Deleting recall your display method (If selectedProblemsAdapter.notifydatasetchanged(); this method is not work)

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50