listview项目单击片段之间的事件?

时间:2015-11-23 09:10:47

标签: android listview android-fragments

我的问题是我有两个相同活动的片段,即片段b和片段c。我的要求是需要将值从c传递给b。来自c的数据必须填充listview中的b我怀疑是当我点击listview上的特定项目时它应该重定向到数据在片段b中注册的点到目前为止我所尝试的是

片段c:

package first.service.precision.servicefirst;

/**
 * Created by 4264 on 23-11-2015.
 */
import android.app.Fragment;
import android.app.FragmentManager;
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.Spinner;

public class NewRequirements extends Fragment {
    Add add;
    customListAdapterldrequirements customListAdapterldrequirements;

    //public static NewRequirements newInstance(Bundle bundle) {

    //     Add myFragment = new Add();
    //    myFragment.setArguments(bundle);
    //}

    // }

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

        View view = inflater.inflate(R.layout.fragment_dialog_claim, container,
                false);
        final Button btnupdate;
        btnupdate = (Button) view.findViewById(R.id.update);
        final Spinner sbu = (Spinner) view.findViewById(R.id.sbuu);
        final ArrayAdapter<CharSequence> adaptersbu = ArrayAdapter.createFromResource(
                getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
        adaptersbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sbu.setAdapter(adaptersbu);
        final Spinner bu = (Spinner) view.findViewById(R.id.bu);
        final ArrayAdapter<CharSequence> adapterbu = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
        adapterbu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        bu.setAdapter(adapterbu);
        final Spinner sbuu = (Spinner) view.findViewById(R.id.sbu);
        final ArrayAdapter<CharSequence> adaptersbuu = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
        adaptersbuu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sbuu.setAdapter(adaptersbuu);
        final Spinner sc = (Spinner) view.findViewById(R.id.sc);
        ArrayAdapter<CharSequence> adaptersc = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
        adaptersc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sc.setAdapter(adaptersc);
        final Spinner ssc = (Spinner) view.findViewById(R.id.ssc);
        ArrayAdapter<CharSequence> adapterssc = ArrayAdapter.createFromResource
                (getActivity().getBaseContext(), R.array.newrequirements, R.layout.spinnerlayout);
        adapterssc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ssc.setAdapter(adapterssc);
        setHasOptionsMenu(true);


        btnupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String str = sbu.getSelectedItem().toString();
                Bundle bundle = new Bundle();
                FragmentManager fm = getFragmentManager();
                Add add = new Add();

                // bundle.putString("yes", str);
                //    Log.i("Bundle", bundle.toString());
                //    Log.v("Add", str);
                // add.setArguments(bundle);
                Main2Activity.mystring=str;
                FragmentTransaction ft = fm.beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.replace(R.id.content_frame, add, "hi");
//               customListAdapterldrequirements.notifyDataSetChanged();
                //add.lv.setVisibility(View.VISIBLE);
                ft.addToBackStack(null);
                ft.commit();


            }

        });
        return view;
    }

}

这是片段b:

package first.service.precision.servicefirst;

/**
 * Created by 4264 on 23-11-2015.
 */
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by 4264 on 16-10-2015.
 */
public  class Add extends Fragment implements View.OnClickListener
{
    private ListView lv;
 static    String  sbu;
    Add add;

    customListAdapterldrequirements cus;
    private ArrayList<LdNewsItem>ldnews;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        try {
            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");




            Bundle bundle=getArguments();

//            sbu = getArguments().getString("yes");


            //    public boolean onTouch(View v, MotionEvent event) {
            //        return false;
            // }

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


            //});

            lv = (ListView) rootView.findViewById(R.id.ldrequirements);
//customListAdapterldrequirements customListAdapterldrequirements=new customListAdapterldrequirements(getActivity(),listContact);

            ArrayList<LdNewsItem> listContact = GetlistContact();
            lv.setAdapter( new  customListAdapterldrequirements(getActivity(), listContact));

         //   cus=new customListAdapterldrequirements(this,ldnews);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                 /*   NewRequirements newRequirements = new NewRequirements();
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

                    //  lv.deferNotifyDataSetChanged();
                    ft.replace(R.id.content_frame, newRequirements, "hi");
                    ft.addToBackStack(null);
                    ft.commit();*/

                }
            });
      /*  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
      //      @Override
        //    public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view

          //      v.getParent().requestDisallowInterceptTouchEvent(true);
//
  //              return false;
    //        }
        });*/
            return rootView;

        }catch (Exception e){
            throw e;
        }
    }




    private ArrayList<LdNewsItem> GetlistContact()
    {
        ArrayList<LdNewsItem> contactlist = new ArrayList<LdNewsItem>();
        //    String str=getArguments().getString("yes");
        sbu=Main2Activity.mystring;

        //   String str="test";
        //  NewRequirements newr=new NewRequirements();
//        String yog=getArguments().getString("sbu");
//        sbu.notify();
        for(int i=1;i<=5;i++) {
            LdNewsItem    contact = new LdNewsItem();
            contact.setSbu(sbu);
            // contact.setReporterName("No matter how big your house is, how recent your car is, or how big your bank account is — our graves will always be the same size. Stay humble." + i);
            contactlist.add(contact);
//lv.setAdapter((ListAdapter) contactlist);

        }

        return contactlist;
    }

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

        Fragment fragment=null;
        setHasOptionsMenu(true);
        Button btnrequirements=(Button)getActivity().findViewById(R.id.btnrequirements);
        btnrequirements.setOnClickListener(this);
        Button btnactnt=(Button)getActivity().findViewById(R.id.btnacnt);
        btnactnt.setOnClickListener(this);
        Button btnprimary=(Button)getActivity().findViewById(R.id.btnprimary);
        btnprimary.setOnClickListener(this);
        Button btnsecondary=(Button)getActivity().findViewById(R.id.btnsecondary);
        btnsecondary.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();
                // 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();
                break;

            case R.id.btnacnt:
                FragmentManager fm=getFragmentManager();
                //FragmentTransaction ft=fm.beginTransaction();
                SelectAccountDialog selectAccountDialog=new SelectAccountDialog();
                selectAccountDialog.setRetainInstance(true);
                selectAccountDialog.show(fm, "fragment_name");

                //ft.addToBackStack(null);

                break;
            case R.id.btnprimary:
                FragmentManager fmm=getFragmentManager();
                // FragmentTransaction ftt=fmm.beginTransaction();
                SelectAccountDialog selectAccountDialog1=new SelectAccountDialog();
                selectAccountDialog1.setRetainInstance(true);
                selectAccountDialog1.show(fmm, "fragment_name");
                //   ftt.addToBackStack(null);
                break;
            case R.id.btnsecondary:
                FragmentManager fm2=getFragmentManager();
                //     FragmentTransaction fttt=fm2.beginTransaction();
                SelectAccountDialog selectAccountDialog2=new SelectAccountDialog();
                selectAccountDialog2.setRetainInstance(true);
                selectAccountDialog2.show(fm2,"fragment_name");
                //     fttt.addToBackStack(null);
                break;

        }

    }



}

这是我的customlistadapter:

package first.service.precision.servicefirst;

/**
 * Created by 4264 on 23-11-2015.
 */
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by 4264 on 12-11-2015.
 */
public class customListAdapterldrequirements extends BaseAdapter{

    Add add;
    private ArrayList<LdNewsItem> listData;
    private LayoutInflater layoutInflater;

    public customListAdapterldrequirements(Context context, ArrayList<LdNewsItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {

        return listData.size();
    }

    @Override
    public Object getItem(int position) {

        return listData.get(position).getSbu();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        View mview = convertView;

        // convertView.setBackgroundColor(Color.parseColor("#E5E5E5"));

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_ld_requirements, null);
            TextView req = (TextView) convertView.findViewById(R.id.txt);
            req.setText("Select Lead Information");
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.sbu);
            holder.reporterNameView = (TextView) convertView.findViewById(R.id.bu);
            holder.reportedDateView = (TextView) convertView.findViewById(R.id.sbuu);
            holder.ldstatusView = (TextView) convertView.findViewById(R.id.sc);
            holder.ldtype = (TextView) convertView.findViewById(R.id.ssc);
            holder.ldsource = (TextView) convertView.findViewById(R.id.rq);
        //    add.lv.setVisibility(View.INVISIBLE);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    //    add.lv.setVisibility(View.VISIBLE);
        holder.headlineView.setText("Strategic Business Unit: " + listData.get(position).getSbu());
        holder.reportedDateView.setText("Business Unit: " + listData.get(position).getSbuu());
        holder.ldstatusView.setText("Sub Business Unit: " + listData.get(position).getSc());
        holder.ldtype.setText("Service Category: " + listData.get(position).getSsc());
        holder.ldsource.setText("Service Sub Category: " + listData.get(position).getReq());


        convertView.setBackgroundColor(Color.parseColor("#E5E5E5"));



        // holder.headlineView.setText("Requirements: " +listData.get(position).getReq());


        return convertView;
    }

    static class ViewHolder {
        public TextView headlineView;
        public TextView reporterNameView;
        public TextView reportedDateView;
        public TextView ldstatusView;
        public TextView ldtype;
        public TextView ldsource;
        public TextView req;
    }
}

即使经过大量研究,我也找不到更好的解决方案?提前谢谢

0 个答案:

没有答案