[英]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.