android - 在 ViewPager fragment 之间传递数据

标签 android android-fragments android-viewpager

为了将值传递给我的 ViewPager 上的其他 fragment ,我正在执行以下操作:

FragmentA.java

//Initial Declaration
ListPasser handler;


public interface ListPasser{
    public void onPodCastClick(int position, String url, String title, String body, String date);
}


//I am implementing a ListView inside the fragment
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.i(tag, mp3URL);
    Log.i(tag, String.valueOf(position));
    Log.i(tag, pod_title);
    Log.i(tag, pod_body);
    Log.i(tag, pod_date);
    handler.onPodCastClick(position, mp3URL, pod_title, pod_body, pod_date);

}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    try {
        handler = (ListPasser) getActivity();

    } catch (ClassCastException e) {
        Log.i(tag,"Activity " + getActivity().getClass().getSimpleName()
            + " does not implement the ElemetsListClickHandler interface");
        e.printStackTrace();
    }
}

MainActivity.java(ViewPager 的实现类)

@Override
public void onPodCastClick(int position, String url, String title,
        String body, String date) {
    // TODO Auto-generated method stub
    Bundle element = new Bundle();
    element.putInt("position", position);
    element.putString("mp3url", url);
    element.putString("title", title);
    element.putString("body", body);
    element.putString("date", date);

    Fragment toGo = new FragmentB();
    toGo.setArguments(element);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.pager, toGo);
    transaction.commit();

}

fragment B.java

TextView npTitle, npDate, npDescription;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.now_playing_layout, container, false);


    npTitle = (TextView)view.findViewById(R.id.npTitle);
    npDate = (TextView)view.findViewById(R.id.npDate);
    npDescription = (TextView)view.findViewById(R.id.npDescription);

    Bundle element = this.getArguments();
    if(element != null){
    String title = element.getString("title");
    String url = element.getString("mp3url");
    String body = element.getString("body");

    npTitle.setText(title);
    npDescription.setText(body);
    }
    else{
        npTitle.setText("Its null man");
        npDescription.setText("NULL POINTER EXCEPTION !");
    }
    return view;
}

当我单击 FragmentA 中的一个项目时,它会转到 FragmentB 并带有界面中显示的信息,但这一切给我的是一个空白屏幕 fragment A。请帮忙。

最佳答案

您可以尝试使用抽象类来保存对象中的值。然后使用静态方法从任何其他类获取值。

关于android - 在 ViewPager fragment 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19172870/

相关文章:

android - fragment 中的 MapView(蜂窝)

android - 使用 FragmentPagerAdapter 时如何获取现有 fragment

android - 不使用 fragment 的操作栏选项卡?

android - 如何使 ViewPager 的项目在方向改变时被销毁?

android - Android 5.0 上的 Jodatime 问题

android - Google Play 应用签名流程

java - 将数据发送到 viewpager 的特定 fragment

android - 查看分页器2/回收器 View 崩溃的不一致

Android fragment 找不到 id 的 View

android - 从 ListView 中的 OnItemLongClickListener 启用拖动排序 ListView 时出现 NullPointerException