无法通过分片改造读取数据

莫妮卡

我尝试从URL获取数据并将其传输到Fragment。该程序可以运行,但是结果什么也没有显示。我认为这是因为我不正确地传输数据。但是我不知道如何解决它。我的控制器类:

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

和我的片段类:

    @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)
@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(...)

相反setArgumentsgetArguments方法是返回设置的bundle对象方法。

请记住,如果您在使用之前不致电,这getArguments可能会返回对于您的情况,我们可以说您应该在提交片段事务之前始终使用nullsetArgumentsgetArgumentssetArguments

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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章