首页 > 解决方案 > 从sqlite数据库中删除后recyclerview没有更新

问题描述

我是安卓新手。我正在为我的应用程序使用内容提供程序,并想从我的 sqlite 数据库中删除一个项目。它会从数据库中删除该项目,但 recyclerview 不会显示它已被删除,除非您在已删除的项目消失之前返回并再次打开活动。我已经查看了代码,但我实际上并不知道问题是什么。

主要活动

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;
    }

}

适配器

  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);

    }
}

任何可以帮助我的人。

标签: androidsqliteandroid-contentprovider

解决方案


对于每次删除,像这样更新您的适配器

selectedProblemsAdapter.notifydatasetchanged();

推荐阅读