简体   繁体   中英

Passing data from listview in fragment to fragment

when i click on the listview the bundle can get the value, but cant pass the value to second fragment. First Fragment

categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
            details_fragment ldf = new details_fragment ();
            Bundle args = new Bundle();
            args.putString("category", category);
            ldf.setArguments(args);
        }
    });
        return view;

}

Second Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view=inflater.inflate(R.layout.my_fragment2, container, false);
    test = (TextView)view.findViewById(R.id.textView);

    Bundle args = getArguments();
    if (args  != null && args.containsKey("category")){
        String userId = args.getString("category");
        test.setText(userId);
    }
    return view;
}

You may be forget to start your second fragment

categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
            details_fragment ldf = new details_fragment ();
            Bundle args = new Bundle();
            args.putString("category", category);
            ldf.setArguments(args);
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.yourContainer, ldf);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        } 
    });
}

and in you second activity

Bundle agrs = getArguments();
if (args != null) { 
    String category = agrs.getString("category");
    test.setText(category);
}

In First Fragment

    categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener()
 {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
            MainActivity main=new MainActivity();
                   main.openSecondFrag();
        }
    });
        return view;

}

In your MainActivity,put openSecondFrag() method

 public void openSecondFrag(){
 details_fragment ldf = new details_fragment ();
                Bundle args = new Bundle();
                args.putString("category", category);
                ldf.setArguments(args);
}

In Second Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view=inflater.inflate(R.layout.my_fragment2, container, false);
    test = (TextView)view.findViewById(R.id.textView);

    Bundle args = getArguments();
    if (args  != null && args.containsKey("category")){
        String userId = args.getString("category");
        test.setText(userId);
    }
    return view;
}

I am sure it will work.

Try this: on your receiving Fragment:

Bundle agrs = this.getArguments();
if (args != null) {
    String myStr = args.getString(key, defaultValue);
}
 `Use this code in your categorylist.setOnItemClickListener` 
    // Create fragment and give it an argument specifying the article it should show
    details_fragment newFragment = new details_fragment ();  
    Bundle args = new Bundle();  
    args.putString("category", category)

     //Note: if your are in fragment than replace getSupportFragmentManager to getFragmentManager.

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
       transaction.replace(R.id.fragment_container, newFragment);  
       transaction.addToBackStack(null);

    // Commit the transaction
       transaction.commit(); 

Use a Bundle. Here's an example:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container_view, bundle).commit();

Bundle has put methods for lots of data types. See this

Then in your Fragment, retrieve the data (eg in onCreate() method) with:

Bundle bundle = this.getArguments();
if (bundle != null) {
    int myInt = bundle.getString(key, defaultValue);
}

HomeActivity.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context="com.example.activity.HomeActivity"
   tools:showIn="@layout/app_bar_home">

//View that will hold all the fragments
<FrameLayout
    android:id="@+id/container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

FirstFragment

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment1, container, false);
    view.setBackgroundColor(Color.WHITE);

     listViewAdapter.SetOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        SecondFragment secondFrag = new SecondFragment();
                        Bundle args = new Bundle();
                        args.putString("Key","some value");

                        secondFrag .setArguments(args);
                        getFragmentManager()
                       .beginTransaction()
                       .replace(R.id.container_view, fragment)
                       .addToBackStack(null)
                       .commit();
                    }
                });
    return view;
}

SecondFragment

public class SecondFragment extends Fragment {

     public SecondFragment() {
    // Required empty public constructor
}

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.second_fragment, container, false);
    TextView textView = (TextView)view.findViewById(R.id.textView);
    view.setBackgroundColor(Color.WHITE);
    String key = getArguments().getString("Key");
    //Set the value to your text view
     textView.setText(key);
    return view;
}

I tried this out and it works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM