简体   繁体   English

如何从另一个活动中删除recycleview中的项目?

[英]how to delete an item in recycleview from another activity?

I am working on an expense tracker app.我正在开发费用跟踪器应用程序。 When I click on item in recycleview, it takes me to another activity where I have two buttons: update and delete.当我单击recycleview 中的项目时,它会将我带到另一个活动,其中有两个按钮:更新和删除。 The issue is I don't get the idea how to delete item from recycleview, which is in another activity, when delete button in that activity is pressed.问题是当按下该活动中的删除按钮时,我不知道如何从另一个活动中的 recycleview 中删除项目。 Actually, I also searched and looked up other similar questions and answers on stack overflow however that didn't help me.实际上,我还搜索并查找了有关堆栈溢出的其他类似问题和答案,但这对我没有帮助。 If someone can help me on this, I will appreciate it greatly.如果有人可以帮助我,我将不胜感激。

This is my code where I am passing the position of my item:这是我的代码,我在其中传递我的项目的 position:

final int pos = holder.getAdapterPosition();
        holder.date.setText(MyModel.getDate());
        holder.note.setText(MyModel.getNote());
        holder.Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(context,Update_transaction.class);
                i.putExtra("key",myViewsList.get(pos).getId());
                i.putExtra("pos",holder.getAdapterPosition());
                context.startActivity(i);

            }
        });

This is the code where I am retrieving the data in another activity:这是我在另一个活动中检索数据的代码:

Bundle bundle = getIntent().getExtras();
        Update_db = new DataSaver(this);
        if(bundle != null)
        {
            id = bundle.getString("key");
            pos = bundle.getString("pos");
            Toast.makeText(this, "pos : " + pos, Toast.LENGTH_SHORT).show();
        }

For interaction between activities, you can use the LocalBroadcastManager.对于活动之间的交互,您可以使用 LocalBroadcastManager。 You can send a broadcast from one activity and receive it in another.您可以从一个活动发送广播并在另一个活动中接收它。

Send broadcast发送广播

val intent = Intent("ACTION")
intent.putExtra("needUpdate", true)
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent)

Receive broadcast接收广播

1.Create a listener 1.创建监听器

val myBroadcastReceiver = object : BroadcastReceiver() {
   override fun onReceive(context: Context?, intent: Intent?) {
       println("Your value: " + intent?.getStringExtra("needUpdate"))
   }
}

Register登记

override fun onResume() {
   // …
   val filter = IntentFilter("ACTION")
   LocalBroadcastManager.getInstance(applicationContext).registerReceiver(myBroadcastReceiver, filter)
}

Cancel registration取消注册

override fun onPause() {
   // …
   LocalBroadcastManager.getInstance(applicationContext).unregisterReceiver(myBroadcastReceiver)
}

More details: https://developer.android.com/guide/components/broadcasts更多详情: https://developer.android.com/guide/components/broadcasts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从活动中从Recycleview中删除项目 - How to remove item from Recycleview from activity 当回收在Fragment上时如何从RecycleView中删除项目? - How to delete item from RecycleView when that recycle is on Fragment? 如何从适配器recycleview传递/获取值复选框到另一个活动 - How to pass/get value checkbox from adapter recycleview to another activity RecycleView 的新活动中单击的项目不正确 - An incorrect clicked item in the new activity from RecycleView 从另一个活动中单击按钮后,从活动中删除列表视图项 - Delete a listview item from activity on button click from another activity 使用 SelectionTracker 删除 RecycleView 中的项目 - Delete item in RecycleView with SelectionTracker 通过数据库从另一个活动中的列表视图中删除项目 - delete an item from listview from another activity through database 如何从另一个活动调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目? - How can I call an ArrayAdapter from another activity to delete item from ArrayAdapter with strings.xml array? 如何从recycleview中删除通知历史记录? - How to delete a notification history from recycleview? 如何将onOptionsItemSelected项目ID从主要活动传递给另一个活动 - how to pass onOptionsItemSelected item id from Main activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM