Android 如何将字符串数组从 RecyclerView 适配器传递到片段
How to Pass String Array from RecyclerView Adapter to Fragment in Android
我正在从适配器中检索值并将值存储在 ArrayList<String>
中。我正在将 ArrayList 转换为字符串数组并将数组从 RecyclerView 适配器传递到其他片段,但我在其他 [=14] 中得到空值=].
这是将 ArrayList<String>
转换为字符串数组并发送到其他片段
的代码
String []tablesId=new String[tableId.size()];
tablesId=tableId.toArray(tablesId);
TableAssignConfirm confirm=new TableAssignConfirm();
Bundle bundle=new Bundle();
bundle.putStringArray("tablesIds",tablesId);
confirm.setArguments(bundle);
这是从其他片段
中的 Bundle
检索值的代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
// Bundle bundle=getArguments().getStringArray("tableIds");
String []getTables=getArguments().getStringArray("tableIds");
Log.i("ids", Arrays.toString(getTables));
return v;
}
如何解决这个问题并获取值?
检查您的 getStringArray()
值。应该是getStringArray("tablesIds")
。下次注意点))
Create an interface in your adapter
public interface OnItemClickListener {
void onItemClicked(int position, ArrayList<DeliveryTimeSlot> list);
}
in your adapter call from fragment
Adapter adapter = new Adapter(context, list, new Adapter.OnItemClickListener() {
@Override
public void onItemClicked(int position, ArrayList<String> list) {
// Handle list and perform actions here
}
});
on your adapter
private OnItemClickListener onItemClickListener; // Global scope
in constructor call:
this.onItemClickListener = onItemClickListener;
on your item clicked event :
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClicked(position, _list);
}
});
试试这个
Bundle bundle=getArguments();
if(bundle!=null){
String []getTables=bundle.getStringArray("tableIds");
}
在您的代码中
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
Bundle bundle=getArguments();
if(bundle!=null){
String []getTables=bundle.getStringArray("tableIds");
}
Log.i("ids", Arrays.toString(getTables));
return v;
}
我正在从适配器中检索值并将值存储在 ArrayList<String>
中。我正在将 ArrayList 转换为字符串数组并将数组从 RecyclerView 适配器传递到其他片段,但我在其他 [=14] 中得到空值=].
这是将 ArrayList<String>
转换为字符串数组并发送到其他片段
String []tablesId=new String[tableId.size()];
tablesId=tableId.toArray(tablesId);
TableAssignConfirm confirm=new TableAssignConfirm();
Bundle bundle=new Bundle();
bundle.putStringArray("tablesIds",tablesId);
confirm.setArguments(bundle);
这是从其他片段
中的Bundle
检索值的代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
// Bundle bundle=getArguments().getStringArray("tableIds");
String []getTables=getArguments().getStringArray("tableIds");
Log.i("ids", Arrays.toString(getTables));
return v;
}
如何解决这个问题并获取值?
检查您的 getStringArray()
值。应该是getStringArray("tablesIds")
。下次注意点))
Create an interface in your adapter
public interface OnItemClickListener {
void onItemClicked(int position, ArrayList<DeliveryTimeSlot> list);
}
in your adapter call from fragment
Adapter adapter = new Adapter(context, list, new Adapter.OnItemClickListener() {
@Override
public void onItemClicked(int position, ArrayList<String> list) {
// Handle list and perform actions here
}
});
on your adapter
private OnItemClickListener onItemClickListener; // Global scope
in constructor call:
this.onItemClickListener = onItemClickListener;
on your item clicked event :
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClicked(position, _list);
}
});
试试这个
Bundle bundle=getArguments();
if(bundle!=null){
String []getTables=bundle.getStringArray("tableIds");
}
在您的代码中
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
Bundle bundle=getArguments();
if(bundle!=null){
String []getTables=bundle.getStringArray("tableIds");
}
Log.i("ids", Arrays.toString(getTables));
return v;
}