android - android通过bundle将数据从片段传递到适配器
问题描述
我使用cardview显示数据并在cardview上有menuItem。单击菜单项后,我将打开一个新片段并希望也传递 id(来自相应的卡)。我尝试使用互联网上提到的不同方式,但我出错了。下面是我用来将数据从片段发送到我的适配器的代码
id = pobj.getString("id");
Intent intent = new Intent(getActivity(), CustomAdapter.class);
intent.putExtra("id", id);
自定义适配器
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
Context mCtx;
private List<Mylist> names;
View view;
SharedPreferences preferences;
String id;
public CustomAdapter(View view,List<Mylist> names,Context mCtx) {
this.names = names;
this.mCtx = mCtx;
this.view =view;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new ViewHolder(v);
}
public void filterList(List<Mylist> filterdNames) {
this.names = filterdNames;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, final int position) {
Mylist mylist = names.get(position);
holder.year.setText(mylist.getPurchase_year());
holder.areatype.setText(mylist.getProperty_type());
holder.area.setText(mylist.getSize());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
//will show popup menu here
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_document:
//handle menu2 click
DocFragment newfragment1 = new DocFragment();
final Intent intent = ((Activity) mCtx).getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
id = bundle.getString("id");
}
Toast.makeText(view.getContext(),id,Toast.LENGTH_LONG).show();
FragmentTransaction fragmentTransaction1 =((AppCompatActivity)mCtx).getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.container_view1, newfragment1);
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
NavigationActivity.container_view1.setVisibility(View.VISIBLE);
break;
}
return false;
}
});
//displaying the popup
popup.show();
解决方案
有两种方法可以将数据从 Activity(或 Fragment 外部)传递到 Fragment :
1-为片段设置参数
Bundle args = new Bundle();
args.setString("id", id);
newfragment1.setArguments(args);
2- 使用 newInstance 函数
在 DocFragment 中创建一个静态方法:
public static DocFragment newInstance(String id) {
DocFragment docFragment = new DocFragment();
Bundle args = new Bundle();
args.putInt("id", id);
docFragment.setArguments(args);
return docFragment;
}
然后将片段的初始化更改为:
String id = bundle.getString("id"); //after retrieving from extras
DocFragment newfragment1 = DocFragment.newInctance(id);
将数据传递给片段后,您可以检索片段的 onCreate 或 onCreateView 方法的参数:
Bundle args = getArguments();
int index = args.getString("id");
推荐阅读
- python - 根据文件中的位置绘制文件中的数字
- elixir - 使用 Phoenix 从 ARC 上传读取二进制数据 -- db 仅显示文件名
- laravel - 所有值都没有插入我的数据库(laravel-5.7)
- sql-server - 多个 SELECTS 进入单个结果行
- python - 为输赢造成延迟
- xilinx - 当被调用函数不可合成时合成调用函数
- php - 在循环中将三个元素增加一个数字
- google-apps-script - Google Scripts - 多重保护的 removeEditors()
- reference - IBM Integration Bus ESQL 对 OutputRoot 的引用不起作用
- c# - C# 和 WPF,当使用 mvvm 样式和按钮操作作为带有命令参数的命令时,如何始终在命令参数中创建新对象?