将值从listfragment发送到另一个片段

时间:2016-01-03 22:23:19

标签: android listview android-fragments parameter-passing

我想将ListFragment中的值发送到另一个片段.....

这是我的代码: MainFragments.java:

public class MainFragments extends ListFragment {
protected List<ParseObject> Ads;
Button btn_LogOut;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main,container, false);

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Ads");
        query.orderByDescending("createdAt");
        query.findInBackground(new FindCallback<ParseObject>() {


        @Override
public void done(List<ParseObject> Adsobjects, ParseException e) {
    if(e==null){
        Ads=Adsobjects;
        AdsAdapter adapter =new AdsAdapter(getListView().getContext(),Ads);
        setListAdapter(adapter);
    }else{

    }

}
});



    btn_LogOut=(Button)rootView.findViewById(R.id.buttonlogout);

    btn_LogOut.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
            ParseUser.logOut();
            updateDetail();

        }
    });
    return rootView;


  }

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ParseObject AdsObject= Ads.get(position);
    String objectId = AdsObject.getObjectId();

    Toast.makeText(getContext(),objectId,Toast.LENGTH_LONG).show();

    Bundle bundle = new Bundle();
    bundle.putString("KEY_DETAIL", objectId);
    MydetailsAdsFragment.setArguments(bundle);
    FragmentTransaction fragmentTransaction =
            getActivity().getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, MydetailsAdsFragment);
    fragmentTransaction.commit();

}

public void updateDetail() {
    Intent intent = new Intent(getActivity(), MainActivity.class);
    startActivity(intent);
}
}

MydetailsAdsFragment:

public class MydetailsAdsFragment extends Fragment {
TextView textDetail;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_details_ads, container, false);
    textDetail = (TextView) rootView.findViewById(R.id.editTxt_det_Titleads);

    Bundle bundle = getArguments();
    if(bundle != null) {
        String detail = bundle.getString("KEY_DETAIL", "no argument pass");
        textDetail.setText(detail);
    }

    return rootView;

}
public void updateDetail(String detail) {
    textDetail.setText(detail);
}

}

我想知道如何解决这个问题?

我们将非常感谢您的帮助!

此致 马尔

1 个答案:

答案 0 :(得分:0)

如果Fragments被同一个Activity膨胀,你应该在Frags中定义Activity将实现的接口,并通过Activity在它们之间传递数据。 PPartisan提供的链接就是一个例子:http://developer.android.com/training/basics/fragments/communicating.html