android 通过包将数据从片段传递到适配器
android passing data from fragment to adapter through 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");
我正在使用 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");