[英]Go back to previous Activity with some `put extra` onClick of a recyclerView Item
I want to pass an data previous activity on click of Item in Recycler view and show it on a Edit Text. 我想在“回收者”视图中单击“项目”时传递数据先前的活动,并将其显示在“编辑文本”上。
This is the code i have used to pass data from listview to the previous activity 这是我用来将数据从列表视图传递到上一个活动的代码
I want to do the same thing with Recyclerview 我想对Recyclerview做同样的事情
//Calling Second Activity //调用第二个活动
public static final int REQUEST_CODE = 100;
Intent dateintent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(dateintent, REQUEST_CODE);
//onClick of listview pass the data back to previous activity // onview of listview将数据传递回上一个活动
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = (TextView) view.findViewById(R.id.textView);
String str = txt.getText().toString();
Intent intent = new Intent();
intent.putExtra("data",str);
setResult(RESULT_OK,intent);
finish();
}
}); });
//After getting data show the data in the first activity edit box //获取数据后,在第一个活动编辑框中显示数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String data= data.getStringExtra("data");
if (data!= null) {
edittext.setText(data);
}
}
}
} }
First create this Interface 首先创建此接口
public interface RunnableValue {
public void run(Object obj);
}
2.This MainActivity add 2.此MainActivity添加
RunnableValue run=new RunnableValue() {
@Override
public Bundle run(Object obj) {
String str = obj.toString();
Intent intent = new Intent();
intent.putExtra("data",str);
setResult(RESULT_OK,intent);
finish();
}
};
mAdapter = new SearchAdapter(dataSet,run);
This RecyclerView Adapter 此RecyclerView适配器
public SearchAdapter(List<String> dataSet,RunnableValue runnableValue) { mDataSet = dataSet; this.runnableValue=runnableValue; } public static class SearchHolder extends RecyclerView.ViewHolder { private final TextView textView; public SearchHolder(View v) { super(v); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { runnableValue.run(getTextView().toString()); } }); textView = (TextView) v.findViewById(R.id.txtSearchItem); } public TextView getTextView() { return textView; }
} }
There is no setOnItemClickListener
available in RecyclerView
, so you need make your own click listener in your RecyclerView
adaper, just check out the post , then you should be able to make it. RecyclerView
没有可用的setOnItemClickListener
,因此您需要在RecyclerView
适配器中创建自己的单击侦听器,只需查看该帖子 ,您就可以做到这一点。
Hope this help! 希望有帮助!
RecyclerView doesn't have a setOnItemClickListener like its predecessor ListView did. RecyclerView没有它的前身ListView那样的setOnItemClickListener。 However, that shouldn't prevent us from doing what we want to do.
但是,这不应阻止我们做我们想做的事情。 So, we reinvent the wheel and make our very own OnItemClickListener for your RecyclerView.
因此,我们重新发明了轮子,并为您的RecyclerView制作了自己的OnItemClickListener。 Here's a step by step guide.
这是逐步指南。
Create an interface called OnItemClickListener by creating a new file called OnItemClickListener.java with an empty method called onItemClick. 通过使用空方法onItemClick 创建一个名为OnItemClickListener.java的新文件,来创建一个名为OnItemClickListener的接口 。
public interface OnItemClickListener { public void onItemClick(View view , int position); }
Create a static variable in your adapter called 在名为的适配器中创建一个静态变量
static OnItemClickListener mItemClickListener;
Setup onClickListener in your custom ViewHolder with a call to our onItemClick method like so 像这样调用我们的onItemClick方法,在自定义ViewHolder中设置 onClickListener
@Override public void onClick(View view) { mItemClickListener.onItemClick(view, getPosition()); }
Create a public method called SetOnItemClickListener in your adapter class 在适配器类中创建一个名为SetOnItemClickListener的公共方法
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) { this.mItemClickListener = mItemClickListener; }
SetOnItemClickListener on your custom RecyclerView Adapter 您的自定义RecyclerView适配器上的SetOnItemClickListener
((NameOfYourAdapter) mAdapter).SetOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View view, int position) { if(view != null) { TextView txt = (TextView) view.findViewById(R.id.textView); String str = txt.getText().toString(); Intent intent = new Intent(); intent.putExtra("data",str); setResult(RESULT_OK, intent); //close this Activity... finish(); } } });
That should do it. 那应该做。 If you have any questions, feel free to ask!
如果你有任何问题随时问!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.