【问题标题】:How to update recyclerview when an activity is deleted which is open from recyclerview adapter删除从recyclerview适配器打开的活动时如何更新recyclerview
【发布时间】:2016-08-23 14:54:40
【问题描述】:

我正在使用 Recyclerview 适配器来填充 Recyclerview。从 SQLite 填充 Recyclerview 后,如果用户想要打开一个 recyclerview 项目,需要单击该项目并适配器打开相关活动。这是一张可以帮助您轻松理解的图像。

当活动打开时,用户可以在删除数据后单击删除按钮从 SQLite 中删除该帖子,recyclerview 应该动态更新数据。

【问题讨论】:

  • 这很简单。从数据库中删除条目后,只需在适配器上调用notifyDataSetChanged。该列表将自行刷新。
  • 但是帖子可以从另一个活动中删除
  • 没关系,从哪里删除都没关系。如果您的RecyclerView 被破坏,它将在您创建它时重新加载数据,并且您的旧条目将不再存在。如果它没有被销毁,则使用现有的RecyclerView 调用notifyDataSetChanged
  • 但我想动态显示数据
  • 我不太明白你的意思。您的帖子说您要动态更新数据。 notifyDataSetChanged 将帮助您做到这一点。也许如果您发布一些代码,诊断您的问题会更容易。

标签: android android-recyclerview android-adapter android-adapterview


【解决方案1】:

您也可以使用 StartActivityForResult 并将第二个活动的结果用于删除第一个活动中的项目。

我的意思是:

  1. FirstActivity 启动 SecondActivity 等待结果
  2. SecondActivity 将结果发送回 FirstActivity。仅当您删除 项目。
  3. 现在 FirstActivity 删除并刷新列表。

在 FirstActivity 中:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

在 SecondActivity 中,当你按下删除按钮时:

Intent returnIntent = new Intent();
returnIntent.putExtra("delete", true);
returnIntent.putExtra("position", position);
setResult(Activity.RESULT_OK, returnIntent);
finish();

最后,FirstActivity 处理结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            if (data.getBooleanExtra("delete") {
                 // get position and delete item from list and refresh
                 int position = data.getIntegerExtra("position");
            }
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

https://stackoverflow.com/a/10407371/1820599

已编辑:

在适配器构造函数中获取 Activity 的上下文:

FirstActivity listener;

public myAdapter(Context context, List<String> items) {
        super(context, R.layout.row_edition, items);

        this.listener = ((FirstActivity) context);
        this.items = items;
    }

然后,在适配器内部,当你推送项目时,调用活动来启动第二个:

listener.startSecondActivity(int position, parameters you need to use);

最后,在您的 FirstActivity 中

startSecondActivity(int position, parameters you need to use) {
    // whatever you have to do
    Intent i = new Intent(this, SecondActivity.class);
    // push position inside intent and whatever you need
    startActivityForResult(i, 1);
}

流程是:

  1. 推送项目
  2. 使用 FirstActivityListener 调用 SecondActivity
  3. 在 SecondActivity 中删除并返回结果
  4. 在 FirstActivity 中,使用辅助方法从适配器中删除项目 内部 que 适配器

【讨论】:

  • 我正在使用适配器打开一个活动,然后使用适配器中的 onActivityResult 更新该适配器的数据
  • 是的,但是你必须在你的活动代码里面管理,然后回复适配器。
  • 好的,我用 recyclerview 活动尝试了这个,但忘记在 adapater 中使用 onActivityResult
  • onActivityResult 应该进入活动内部,而不是适配器内部。活动将处理结果。
  • 但是如何在适配器中使用 startActivityForResult ?
【解决方案2】:

如果您在单击以显示公司的详细信息后在 recyclerview 中显示公司列表,并且您一旦返回删除公司,您应该会发现该项目消失了这是我的代码所做的

 protected void onResume()
{
    super.onResume();
    Log.i("TAG", "resume");
    if(yourlist.size() > 0)
    {
        yourlist.clear();
        yourlist.addAll(where your data come from 
        ex:databaseHelper.GetOrganization());
        youradapter.notifyDataSetChanged();
    }
}

【讨论】:

  • 欢迎来到 SO。请考虑解释为什么以及如何帮助您的代码。 Code&only 答案被认为是一种不好的风格
【解决方案3】:

您必须在您的活动中实现一个侦听器,它告诉您的回收站视图项目已更改。我想您已经为回收站视图实现了自己的 onItemClickListener,因此您有位置并且可以轻松地从回收站视图数据集中删除项目。如需更多信息,请发布您的代码。

此侦听器进入填充 Recycler 视图的类中。

     public interface DeletedListener {
         void deleted(int position);
     }

使您的活动实现此侦听器,并在那里发送必须删除的位置。

    public void setListener(DeletedListener listener) {
       this.listener = listener;
    }

    DeletedListener listener;

从您的活动调用 setListener 方法,并从适配器调用已删除的方法。

【讨论】:

  • 是的,我使用了 onItemClickListener,但你能告诉我如何在活动中使用 Listener
  • 您必须在适配器中维护和 DeletedListener 实例。我更新了我的答案。
  • 你无法在适配器中处理 onActivityResult。只在活动中。 developer.android.com/reference/android/app/…, int, android.content.Intent)
猜你喜欢
  • 1970-01-01
  • 2021-01-24
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2018-11-23
  • 2020-07-27
相关资源
最近更新 更多