How do I change the content of a Fragment when transferring data from the Fragment after being retrieved to the RecyclerView?
I am fetching news from an API in a Business Fragment and I am migrating that data to BusinessDetail. But when I click on certain news, it shows me the same news. I cannot change the data; even if I install apk again it will show me the same old news.
Some codes have been omitted.
Primary activity:
public void send(StringList stringList) {
bundle = new Bundle();
bundle.putParcelable("businessnews", stringList);
BusinessDetail businessDetail = new BusinessDetail();
businessDetail.setArguments(bundle);
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
Business:
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
StringList stringList;
Transfer transfer;
private RecyclerView recyclerView;
public Business() {
}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transfer.send(stringList);
Log.d("ashu","business onclick stringlist value: "+stringList);
}
});}}}
BusinessDetail:
public class BusinessDetail extends Fragment {
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
Bundle bundle=getArguments();
if (bundle==null || !bundle.containsKey("businessnews")){
throw new IllegalArgumentException("stringlist should not be null");
}
StringList stringList=bundle.getParcelable("businessnews");
Log.d("ashu","stringlist value business detail"+stringList);
authorSecond.setText(stringList.authorName);
headlineSecond.setText(stringList.headline);
detailsSecond.setText(stringList.newsDetail);
}}
StringList:
public class StringList implements Parcelable{
public String authorName;
public String headline;
public String publishedTime;
public String newsDetail;
protected StringList(Parcel in) {
authorName = in.readString();
headline = in.readString();
publishedTime = in.readString();
newsDetail = in.readString();
}
public static final Parcelable.Creator<StringList> CREATOR = new Parcelable.Creator<StringList>() {
@Override
public StringList createFromParcel(Parcel in) {
return new StringList(in);
}
@Override
public StringList[] newArray(int size) {
return new StringList[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(authorName);
parcel.writeString(headline);
parcel.writeString(publishedTime);
parcel.writeString(newsDetail);
}
}
Adapter code:
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
int prevposition = 0;
private List<StringList> c;
public BusinessAdapter(Business context, List<StringList> result) {
c = context.businessNews;
}
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
if (position > prevposition) {
AnimationClass.animate(holder, true);
} else {
AnimationClass.animate(holder, false);
}
}
@Override
public int getItemCount() {
return c.size();
}
}
source to share
why are you sending the "stringList" field every time?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
StringList stringList;
Transfer transfer;
private RecyclerView recyclerView;
public Business() {
}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringList v = businessNews.get(getAdapterPosition());
transfer.send(v);
Log.d("ashu","business onclick stringlist value: "+stringList);
}
});}}}
source to share
Primary activity:
StringList mStringList;
public void send(StringList stringList) {
setStringList(stringList);
BusinessDetail businessDetail = new BusinessDetail();
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
void setStringList(StringList stringList){
mStringList = stringList;
}
public StringList getStringList(){
return mStringList;
}
BusinessDetail:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
StringList stringList = ((MainActivity)getActivity()).getStringList();
Log.d("ashu","stringlist value business detail"+stringList);
authorSecond.setText(stringList.authorName);
headlineSecond.setText(stringList.headline);
detailsSecond.setText(stringList.newsDetail);
}
------------------------------------------- or --- --- ----------------
MainActivity:
public void send(StringList stringList) {
bundle = new Bundle();
bundle.putParcelable("businessnews", stringList);
BusinessDetail businessDetail = new BusinessDetail(bundle);
frag = getSupportFragmentManager();
frag.beginTransaction().replace(R.id.content_frame, businessDetail).commit();
}
BusinessDetail:
public class BusinessDetail extends Fragment {
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
public BusinessDetail(Bundle b){
BusinessDetail bd = new BusinessDetail();
bd .setArguments(b)
return bd;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
.
.
.
source to share
From what I have been able to figure out so far, it looks like you are holding a reference to an old object StringList
that has probably never been updated. Also, you also referenced the old onClickListener
one as you only set it once when you created the class BusindesHolder
. I would recommend the following:
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
//StringList stringList; //You may be holding a reference to the wrong object. Don't use this here.
Transfer transfer;
private RecyclerView recyclerView;
public Business() {}
public interface Transfer {
public void send(StringList stringList);
}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
public void bindListName(final StringList m, int position) {
//YOUR CODE HERE....
//You were passing the onClickListener only once, and it could have been holding a reference to the wrong StringList object. Move that code here instead.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
transfer.send(m);
Log.d("ashu","business onclick stringlist value: " + m);
}
});
}
}
}
Move itemView.setOnClickListener()
to your method bindListName
and just use the object passed here StringList
. This way, whenever your cell is RecyclerView
updated with the method onBindViewHolder
, it is updated with the latest information.
source to share