Reputation: 9
i have a main activity and alot with fragments in it .. now i want to pass data between fragment to another fragment Help me with this ...
try to use intent but its never did what i want and errors occures
this is my source fragment :
public class SubCategoryDetail extends Fragment {
View view;
SubCategory subCategory;
SwipeRefreshLayout swipeRefreshLayout;
RecyclerView includedView, relatedView, popularView;
TextView titlePricing, titleIncluded, pricing1, pricing2;
Spinner spinner;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.subcategory_detail, container, false);
bindViews();
((HomeActivity) getActivity()).visibility(false, false, false);
return view;
}
private void bindViews() {
((HomeActivity) getActivity()).toolbar.setVisibility(View.VISIBLE);
Bundle bundle = getArguments();
if (bundle != null) {
subCategory = (SubCategory) bundle.getSerializable("data");
}
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
spinner = (Spinner) view.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new ItemSelectedListener());
getDetails();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
getDetails();
}
});
}
Glide.with(getActivity()).load(subCategory.getImage()).into(bannerTop);
title.setText(subCategory.getName());
ImageAdapter imageAdapter = new ImageAdapter(subCategory.getImages(), getActivity());
recyclerView.setAdapter(imageAdapter);
description.setText(subCategory.getDescription());
IncludedAdapter includedAdapter = new IncludedAdapter(subCategory.getInclude(), getActivity());
includedView.setAdapter(includedAdapter);
PopularAdapter popularAdapter = new PopularAdapter(subCategory.getPopular(), getActivity());
popularView.setAdapter(popularAdapter);
TypefaceHelper.typeface(title, MyApplication.getBold());
TypefaceHelper.typeface(description, MyApplication.getLight());
TypefaceHelper.typeface(allProvider, MyApplication.getRegular());
TypefaceHelper.typeface(titleIncluded, MyApplication.getRegular());
TypefaceHelper.typeface(titlePricing, MyApplication.getRegular());
TypefaceHelper.typeface(pricing1, MyApplication.getLight());
TypefaceHelper.typeface(pricing2, MyApplication.getLight());
}
private void disableViews(Boolean what) {
for (int i = 0; i < swipeRefreshLayout.getChildCount(); i++) {
View child = swipeRefreshLayout.getChildAt(i);
child.setEnabled(what);
}
}
public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.Holder> {
private List<Category> list;
private Context context;
public PopularAdapter(List<Category> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public PopularAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new PopularAdapter.Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.popular_item, parent, false));
}
@Override
public void onBindViewHolder(PopularAdapter.Holder holder, int position) {
final Category category = list.get(position);
Glide.with(context).load(category.getImage())
.apply(new RequestOptions()
.placeholder(R.drawable.home)
.error(R.drawable.home)
.centerCrop())
.into(holder.icon);
holder.name.setText(category.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("data", category);
bundle.putString("from", "one");
bundle.putString("name", category.getName());
SubCategoryFragment fragment = new SubCategoryFragment();
fragment.setArguments(bundle);
((HomeActivity) context).changeFragment(fragment, "Sub Category");
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class Holder extends RecyclerView.ViewHolder {
ImageView icon;
TextView name;
public Holder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.category_item);
name = (TextView) itemView.findViewById(R.id.cat_name);
TypefaceHelper.typeface(name, MyApplication.getLight());
}
}
}
Mainly this is what i want to pass !!
public class ItemSelectedListener implements AdapterView.OnItemSelectedListener {
//get strings of first item
String firstItem = String.valueOf(spinner.getSelectedItem());
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(pos == 0) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
}
else if(pos == 1) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 2) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 3) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 4) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg) {
}
}
}
please tell me the way that i can pass this data to a textview in the second fragment
Upvotes: 0
Views: 60
Reputation: 11
If you want to pass the data to a fragment say myFragment then see snippet below
Bundle bundle=new Bundle();
bundle.putString("message",msg); //here msg is value you want to pass
myFragment fragment=new myFragment();
fragment.setArguments(bundle);`
now to access this in myFragment class using code (generally declared in oncreate() of fragment class):
Bundle bundle=getArguments();
if(bundle!=null){
Textiew textView.setText(String.valueOf(bundle.getString("message")));
}
Upvotes: 1