当从RecyclerView适配器打开的活动被删除时,如何更新RecyclerView

4
我正在使用Recyclerview适配器来填充Recyclerview。从SQLite填充Recyclerview后,如果用户想要打开一个recyclerview项,需要单击该项,然后适配器将打开相关的活动。下面的图片可以帮助您更容易地理解。

Updating RecyclerView

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


这很简单。在从数据库中删除条目后,只需在适配器上调用notifyDataSetChanged即可。列表将自动刷新。 - Pztar
但是帖子可以从另一个活动中删除。 - Harish Kamboj
没问题,从哪里删除都无所谓。如果您的RecyclerView被销毁,当您重新创建它时,它将重新加载数据,您的旧条目将不再存在。如果它没有被销毁,那么请使用现有的RecyclerView调用notifyDataSetChanged - Pztar
但我想要动态地显示数据。 - Harish Kamboj
我不太明白你的意思。你的帖子中说你想动态更新数据。notifyDataSetChanged 可以帮助你实现这个功能。如果你能发一些代码,也许更容易诊断你的问题。 - Pztar
我没有使用任何代码,因为我不知道如何处理从一个与适配器相同的活动打开并更新适配器。 - Harish Kamboj
3个回答

2

您也可以使用 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://dev59.com/t2kv5IYBdhLWcg3wtS8N#10407371

编辑:

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

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中使用适配器内的辅助方法从适配器中删除项目

我正在使用适配器打开一个活动,然后更新该适配器的数据,这是否可以通过适配器中的 onActivityResult 实现? - Harish Kamboj
好的,我尝试使用RecyclerView活动,但是忘记在适配器中使用onActivityResult - Harish Kamboj
onActivityResult 应该放在 Activity 内部,而不是 Adapter 内部。Activity 将处理结果。 - adalpari
但是如何在适配器中使用startActivityForResult? - Harish Kamboj
listener.startSecondActivity(int position, parameters you need to use); 如何使用这个 - Harish Kamboj
显示剩余2条评论

1
如果您在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();
    }
}

1
欢迎来到SO。请考虑解释您的代码如何以及为什么有帮助。仅包含代码的答案被认为是一种不好的风格。 - Neuron

0

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

这个监听器放在你填充回收视图的类中。

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

使您的活动实现此侦听器,并在那里发送要删除的位置。
    public void setListener(DeletedListener listener) {
       this.listener = listener;
    }

    DeletedListener listener;

从您的活动中调用setListener方法,从适配器中调用deleted方法。

是的,我使用了onItemClickListener,但是你能告诉我如何在活动中使用Listener吗? - Harish Kamboj
你必须在你的适配器中维护一个DeletedListener实例。我更新了我的答案。 - Carles Arnal Castelló
你不能在适配器中处理onActivityResult,只能在活动中处理。https://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent) - Carles Arnal Castelló

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接