如何将数据从recyclerview适配器传递到片段,以及我必须在片段中编写代码以接收数据。我已经检查了本网站中的链接,但不幸的是无法获取正确的代码。 下面是代码
ExampleAdapter.Java
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item,viewGroup,false);
ExampleViewHolder evh=new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, int i) {
final ExampleItem currentItem=mExampleList.get(i);
exampleViewHolder.mTextView.setText(currentItem.getmText());
exampleViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// pass the data to the fragment
}
});
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ExampleViewHolder(@NonNull View itemView) {
super(itemView);
mTextView=itemView.findViewById(R.id.texttitle);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList){
this.mExampleList=exampleList;
}
}
答案 0 :(得分:0)
您有两个选择;
使用GET my_index/mydocs/_search
{
"query": {
"match": {
"name": "gb"
}
}
}
:
SharedPreferences
获得:
Context myContext = exampleViewHolder.itemView.getContext()
SharedPreferences.Editor editor = getSharedPreferences("DeviceToken", MODE_PRIVATE).edit();
editor.putString("YourKeyNameHere", currentItem.getmText()); // or add toString() after if needed
editor.apply();
使用SharedPreferences prefs = getSharedPreferences("DeviceToken", MODE_PRIVATE);
String deviceToken = prefs.getString("YourKeyNameHere", null); // get it here
:(如果要在传递数据时启动另一个Intent
)
Activity
获得:
Intent intent = new Intent(myContext , SignoutActivity.class);
intent.putExtra("EXTRA_TEXT", currentItem.getmText());
startActivity(intent); // if needed, add myContext before starting myContext.startActivity...
答案 1 :(得分:0)
您的片段中包含“ ClickListener”。
例如。
@Override
public void onResume(){
super.onResume();
((ExampleAdapter) mAdapter).onItemClick(new RecyclerViewItemClick() {
@Override
public void onItemClick(int position, View v) {
//to do. ArrayList<> data.get(position).getYourItem();
}
});
}
并且您必须创建接口RecyclerViewItemClick.java
public interface RecyclerViewItemClick {
public void onItemClick(int position, View v);
}
最后。 ExampleAdapter.java添加
public void onItemClick(RecyclerViewItemClick mclick){
this.myClickListener = mclick;
}
ExampleViewHolder方法添加
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
notifyDataSetChanged();
}
希望您能解决此问题。谢谢。
答案 2 :(得分:0)
具有一个IDataCollector
界面。在Fragment
中实现她,将Fragment
引用作为构造函数参数,并在适配器内部使用
public interface IDataCollector {
void setText(String text);
void setAnything(Object anything);
}
// Fragment
public class Fragment implements IDataCollector {
private void initView() {
// Add this as a parameter
ExampleAdapter adapter = new ExampleAdapter(... , this);
}
@Override
public void setText(String text){
this.textField = text;
}
@Override
public void setText(Object anything){
this.anything = anything;
}
}
// Adapter code
// Constructor, parameters ommited
public ExampleAdapter(...., IDataCollector dataCollector) {
this.dataCollector = dataCollector;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, int i) {
final ExampleItem currentItem=mExampleList.get(i);
exampleViewHolder.mTextView.setText(currentItem.getmText());
exampleViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataCollector.setText(((TextView)v.findViewById(R.id.label1)).getText());
....
}
});
}
答案 3 :(得分:0)
您可以通过将回调附加到适配器来传递数据:
创建界面:
interface OnTextClickListener {
void onTextClick(ExampleItem data);
}
删除回调:
class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
List<ExampleItem> items;
OnTextClickListener listener;
ExampleAdapter(List<ExampleItem> items, OnTextClickListener listener) {
this.items = items;
this.listener = listener;
}
@Override
public void onBindViewHolder(ExampleViewHolder viewHolder, int position) {
viewHolder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Say you want to pass an ExampleItem back to the fragment...
ExampleItem data = items.get(position);
listener.onTextClick(data);
}
});
}
}
使用界面实现片段:
class YourFragment extends Fragment implements OnTextClickListener {
public onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView.setAdapter(new ExampleAdapter(this));
}
void onTextClick(ExampleItem data) {
// Now you can do however you want with the data here...
Toast.makeText(getActivity(), "Got: " + data, Toast.LENGTH_SHORT).show()
}
}
答案 4 :(得分:0)
通过界面,您可以解决此问题 1.在适配器中创建一个接口,添加所需的方法 2.随时在适配器中调用这些方法 3.在片段或活动中实现接口,将上下文传递给适配器