Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the content of fragment when transfering data from fragment after fetching in RecyclerView?

I am fetching news from API in business fragment and I am transferring that data to the BusinessDetail. But when I click on a particular news it show me the same news. I can't the change the data; even if I install the apk again, it shows me the same old news.

Some code has been omitted.

Main 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();
    }
}

2 Answers

why you send "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);
                }
            });}}}
like image 138
wanpen Avatar answered Nov 30 '25 11:11

wanpen


Main 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;
}
.
.
.
like image 36
배준모 Avatar answered Nov 30 '25 11:11

배준모



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!