gpt4 book ai didi

android - 在 ViewPager fragment 之间传递数据

转载 作者:行者123 更新时间:2023-11-30 03:20:50 24 4
gpt4 key购买 nike

为了将值传递给我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com