1

I try to get data from the URL and transfer it to Fragment. The program works but as a result, nothing is displayed. I think it is because I am transferring data incorrectly. But I don't know how to fix it. my controller class:

public class Controller implements Callback<News> {
    private static final String BASE_URL = "https://news.com/wwer/";
    private ApiInterface apiInterface;
    private MainActivity mainActivity;

    public Controller(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }
    public void start() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiInterface = retrofit.create(ApiInterface.class);

        Call<News> news = apiInterface.getNews();
        news.enqueue(this);
    }

    @Override
    public void onResponse(Call<News> call, Response<News> response) {
        News news = response.body();

        String sourceName = news.getSources().get(1).getName();
        Bundle bundle = new Bundle();
        bundle.putString("sourceName", sourceName);

        FragmentManager fm = mainActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        NameFragment fragmentCurrent = new NameFragment();
        fragmentCurrent.setArguments(bundle);
        ft.add(R.id.fragment,fragmentCurrent);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public void onFailure(Call<News> call, Throwable t) {
        Log.d("error", "can't parse data: ", t);
    }
}

and my fragment class:

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.name_fragment, container, false);
        TextView textView = view.findViewById(R.id.news_names_list);
        Bundle bundle = new Bundle();
        String sourcNam = bundle.getString("sourceName");

        textView.setText(sourcNam);
        return view;
    }
}
Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
Monica
  • 384
  • 2
  • 4
  • 16

1 Answers1

2
@Override
public void onResponse(Call<News> call, Response<News> response) {
    ...
    Bundle bundle = new Bundle();
    ...
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ...
    Bundle bundle = new Bundle();
    ...
}

These two bundles that you create using new keyword are two absolutely different objects. It means if you put in one bundle some argument the other bundle will not contain it.

Solution

What you need to modify is how you extract the value from a bundle that you used as the argument to the fragment.setArguments(...) method.

The opposite of the setArguments is the getArguments method that returns you the bundle object you set.

Remember, that getArguments can return null if you do not call setArguments before using the getArguments. In your case we could say that you should always use setArguments before committing a fragment transaction.

public class NameFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.name_fragment, container, false);
        TextView textView = view.findViewById(R.id.news_names_list);
        
        String sourcNam = getArguments().getString("sourceName");

        textView.setText(sourcNam);
        return view;
    }
}
Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
  • 1
    thank you for your advice. Now in fragment class I do this: `Bundle bundle = this.getArguments(); if (bundle!=null) { String sourcNam = bundle.getString("sourceName"); textView.setText(sourcNam); }` – Monica Oct 20 '20 at 20:37
  • @JeneaVranceanu need help here --> https://stackoverflow.com/questions/64495233/retrofit-call-inside-an-adapter-in-android – android dev Oct 23 '20 at 09:37