无法使用 Retrofit to Fragment 读取数据
Can't read data with Retrofit to Fragment
我尝试从 URL 获取数据并将其传输到 Fragment。该程序可以运行,但结果没有显示任何内容。我认为这是因为我传输数据不正确。但我不知道如何解决它。
我的控制器 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);
}
}
和我的片段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;
}
}
@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();
...
}
您使用 new
关键字创建的这两个包是两个完全不同的对象。这意味着如果你在一个包中放入一些参数,另一个包将不会包含它。
解决方案
您需要修改的是如何从用作 fragment.setArguments(...)
方法参数的包中提取值。
与 setArguments
相反的是 getArguments
方法,它 return 将您设置的捆绑对象发送给您。
请记住,如果您在使用 getArguments
之前不调用 setArguments
,那么 getArguments
可以 return null
。在您的情况下,我们可以说您应该始终在提交片段事务之前使用 setArguments
。
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;
}
}
我尝试从 URL 获取数据并将其传输到 Fragment。该程序可以运行,但结果没有显示任何内容。我认为这是因为我传输数据不正确。但我不知道如何解决它。 我的控制器 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);
}
}
和我的片段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;
}
}
@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();
...
}
您使用 new
关键字创建的这两个包是两个完全不同的对象。这意味着如果你在一个包中放入一些参数,另一个包将不会包含它。
解决方案
您需要修改的是如何从用作 fragment.setArguments(...)
方法参数的包中提取值。
与 setArguments
相反的是 getArguments
方法,它 return 将您设置的捆绑对象发送给您。
请记住,如果您在使用 getArguments
之前不调用 setArguments
,那么 getArguments
可以 return null
。在您的情况下,我们可以说您应该始终在提交片段事务之前使用 setArguments
。
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;
}
}