【问题标题】:Recyclerview is not updated after deleting from the sqlite database从sqlite数据库中删除后recyclerview没有更新
【发布时间】:2019-01-15 03:51:11
【问题描述】:

我是安卓新手。我正在为我的应用程序使用内容提供程序,并想从我的 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);

    }
}

任何可以帮助我的人。

【问题讨论】:

  • 您删除值的位置向我们显示您的删除代码

标签: android sqlite android-contentprovider


【解决方案1】:

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

selectedProblemsAdapter.notifydatasetchanged();

【讨论】:

  • 我应该将它添加到主要活动中吗?请帮我写一些代码
  • @oshabzChuks 你应该自己研究一下,它已经在 stackoverflow 中可用,自己挖掘而不是要求代码
  • @AngusTay 我对此做了很多研究。我在我的适配器上添加了 notifydatasetchanged() 但它似乎不起作用。这就是我决定问这个问题的原因。
  • 不要放入适配器。
【解决方案2】:

假设您的所有删除过程都是正确的,并且您希望在 RecyclerView 中的整个列表中显示动画。

而不是使用 notifyItemRemoved(位置) 你可以实现

notifyItemRangeChanged(position, getItemCount());

notifyItemRangeChanged() 通知 RecyclerView 适配器适配器中元素的位置已从位置更改。

Here 是您的参考。

希望对您有所帮助!

【讨论】:

  • 它为 recyclerview 设置动画,但不会从 recyclerview 中删除该项目
  • @oshabzChuks 如果它为recyclerview设置了动画,这意味着它已经调用了该函数,指的是“不删除项目”,我猜你甚至没有执行删除操作,你检查你的sqlite数据库吗数据?
  • 是的,我已经用数据库浏览器检查了它的 sqlite 行已从数据库中删除,但显示在 recyclerview 上
【解决方案3】:

问题可能是删除数据库中的记录后列表没有重新加载。您可以尝试getLoaderManager().restartLoader(int id, Bundle args, LoaderCallbacks&lt;D&gt; callback) 方法,这应该是重新启动查询并从数据库更新它。但愿如此。在here查看更多信息

【讨论】:

    【解决方案4】:

    删除后调用你的显示方法 (如果selectedProblemsAdapter.notifydatasetchanged();这个方法不行)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 2016-09-17
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多