简体   繁体   English

在片段之间的列表视图中传递数据

[英]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 在您的MainActivity中,放入openSecondFrag()方法

 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. Bundle为许多数据类型放置了方法。 See this 看到这个

Then in your Fragment, retrieve the data (eg in onCreate() method) with: 然后在您的片段中,使用以下方法检索数据(例如,在onCreate()方法中):

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

HomeActivity.xml 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 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. 我尝试了一下,它运行完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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