如何将微调器值从一个片段传递到另一个片段?

时间:2015-11-12 13:51:35

标签: android android-fragments android-listview

对于Android开发新手,我的要求是需要从一个片段中传递微调器的值  到另一个并在tofragment中的listview中填充该值:

这是我的fromfragment:

  final String god=sbu.getSelectedItem().toString();
    btnupdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    //     
           Add yog=new Add();
            Bundle args=new Bundle();
            args.putString("yog",god);
            yog.setArguments(args);
            getFragmentManager().beginTransaction().add(R.id.content_frame,yog).commit();
        }
    });
    return view;

这是我的承诺:

package com.example.first.servicefirst;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
 import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;


public  class Add extends Fragment implements View.OnClickListener
{
public static Add()
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_btn_add, container, false);
    Spinner ldsource=(Spinner)rootView.findViewById(R.id.lead_source);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox1, R.layout.spinnerlayout);


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ldsource.setAdapter(adapter);
 //   EditText editText=(EditText)rootView.findViewById(R.id.title);

    Spinner ldtype=(Spinner)rootView.findViewById(R.id.ldtype);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox2,R.layout.spinnerlayout);

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ldtype.setAdapter(adapter1);
    Spinner ldstatus=(Spinner)rootView.findViewById(R.id.ldstatus);
    TextView txt=(TextView)rootView.findViewById(R.id.spinnerTarget);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            getActivity().getBaseContext(), R.array.dropbox3, R.layout.spinnerlayout);

    adapter2.setDropDownViewResource(android.R.layout.simple_selectable_list_item);
    ldstatus.setAdapter(adapter2);
  //  Bundle bundle=getArguments();
  //  String good=bundle.getString("sbu");
    ArrayList<LdNewsItem> listContact = GetlistContact();
    final   ListView lv = (ListView)rootView.findViewById(R.id.ldrequirements);
    lv.setAdapter(new customListAdapterldrequirements(getActivity(), listContact));
//    lv.setOnTouchListener(new View.OnTouchListener() {
  //      @Override
    //    public boolean onTouch(View v, MotionEvent event) {
    //        return false;
       // }

        // Setting on Touch Listener for handling the touch inside ScrollView


    //});
     return rootView;


}




private ArrayList<LdNewsItem> GetlistContact()
{

    ArrayList<LdNewsItem> contactlist = new ArrayList<>();
    LdNewsItem contact = new LdNewsItem();
    String yog=getArguments().getString("sbu");

    for(int i=1;i<=10;i++)
    {
       // contact = new LdNewsItem( );
        contact.setSbu(""+yog);
       // contact.setBu(""+str);
       // contact.setSbuu("Yogeswaran" + str);
        contact.setSc("Sales" + i);
        contact.setSsc("term" + i);
        contact.setReq("business"+i);
        contactlist.add(contact);
    }

    return contactlist;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Fragment fragment=null;

    Button btnrequirements=(Button)getActivity().findViewById(R.id.btnrequirements);
    btnrequirements.setOnClickListener(this);

}
@Override

public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnrequirements:
            Fragment newFragment = new NewRequirements();
            // consider using Java coding conventions (upper first char class names!!!)
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            FragmentTransaction ft = getActivity()
                    .getFragmentManager().beginTransaction();

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

            // Commit the transaction
            transaction.commit();

    }
}



}

2 个答案:

答案 0 :(得分:1)

最受欢迎的方式。

  1. 您可以使用eventBus。
  2. 创建自定义侦听器。

答案 1 :(得分:0)

好吧,你可以简单地把它放在一个静态变量中:

public static String[] spinner_items = new String[5];

以及如何获得它:

String[] hold_array =  getResources().getStringArray(R.array.dropbox3)
ldtype.setOnItemSelectedListener(OnSpinnerItemSelected);
 private OnItemSelectedListener OnSpinnerItemSelected = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {

             if(parent.getId() == ldtype.getId()){
                  spinner_item[0] = hold_array[pos];
             }else if(parent.getId() == ldtype1.getId()){
                  spinner_item[1] = hold_array1[pos];
             }else if(parent.getId() == ldtype2.getId()){
                  spinner_item[2] = hold_array2[pos];
             }else if(parent.getId() == ldtype3.getId()){
                  spinner_item[3] = hold_array3[pos];
             }else if(parent.getId() == ldtype4.getId()){
                  spinner_item[4] = hold_array4[pos];
             }

        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

然后您只需从任何其他活动或片段中访问spinner_item变量。