动态列表视图不填充?

时间:2015-11-24 14:11:15

标签: android listview

我为android开发的新功能我的要求是我有两个片段即a和b frag a是父片段,frag b是孩子我怀疑是当我从b传递数据到它应该在frag ai中动态填充listview使用notifydatasetchange,但它不能正常工作我的代码是

frag b(from_frag):

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.EditText;
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 EditText requiremtns=(EditText)view.findViewById(R.id.txtEmployeeCode);
        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();
       String business_unit=         bu.getSelectedItem().toString();
                String sub_business_unit=sbuu.getSelectedItem().toString();
   String service=sc.getSelectedItem().toString();
                String servicec=ssc.getSelectedItem().toString();

                String req=requiremtns.getText().toString();
                FragmentManager fm = getFragmentManager();
                Add add = new Add();

                // bundle.putString("yes", str);
                //    Log.i("Bundle", bundle.toString());
                //    Log.v("Add", str);
                // add.setArguments(bundle);
                Main2Activity.sbu=str;
                Main2Activity.bu=business_unit;
                Main2Activity.sbuu=sub_business_unit;
                Main2Activity.sc=service;
                Main2Activity.ssc=servicec;
                Main2Activity.req=req;
                FragmentTransaction ft = fm.beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.replace(R.id.content_frame, add, "hi");

                //add.lv.setVisibility(View.VISIBLE);
                ft.addToBackStack(null);
                ft.commit();
                //Add add1=(Add)getFragmentManager().findFragmentByTag(Add.TAG);
Add add1=new Add();
         //       add1.updateListView();

  //customListAdapterldrequirements.notifyDataSetChanged();
            }

        });
        return view;
    }
  /*  @Override
    public void onDestroy() {
        Add add = new Add();
        // TODO Auto-generated method stub
        super.onDestroyView();
        Leads leads=new Leads();
        FragmentManager back=getFragmentManager();
        FragmentTransaction ft=back.beginTransaction();
        ft.replace(R.id.content_frame,leads);
        back.popBackStack();

    }*/
}

Fragagment a(to_frag):

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.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 {
   public static ListView lv;
    static String sbu, sbuu, sc, ssc, req;
    static String bu;
    public static String TAG="Add";
customListAdapterldrequirements customListAdapterldrequirements;
    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");
//getFragmentManager().popBackStack();

            Bundle bundle = getArguments();
                 ListView     lv = (ListView)rootView.findViewById(R.id.ldrequirements);
//            sbu = getArguments().getString("yes");


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

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


            //});
try {




            // customListAdapterldrequirements customListAdapterldrequirements=new customListAdapterldrequirements(getActivity(),listContact);
           /* 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);

                    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;
    //        }
        });*/

       //     updateListView();

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

        } catch (Exception e) {
            throw e;
        }
    }catch (Exception e){
        throw e;}
    }
 public void updateListView() {



    }


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

        sbu = Main2Activity.sbu;
        bu = Main2Activity.bu;
        sbuu = Main2Activity.sbuu;
        sc = Main2Activity.sc;
        ssc = Main2Activity.ssc;
        req = Main2Activity.req;
      {
            LdNewsItem contact = new LdNewsItem();
            contact.setSbu(sbu);
            contact.setBu(bu);
            contact.setSbuu(sbuu);
            contact.setSc(sc);
            contact.setSsc(ssc);
            contact.setReq(req);
            // 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);

            // contact.setReporterName(" LdNewsItem contact1 = new LdNewsItem();


        }

        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;

        }

    }


/*@Override
    public void onDestroy() {
       Add add = new Add();
        // TODO Auto-generated method stub
        super.onDestroyView();

FragmentManager back=getFragmentManager();
        FragmentTransaction ft=back.beginTransaction();
   //  ft.replace(R.id.content_frame, leads);
   back.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    }*/

}

我的惯例如下:

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.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 4264 on 12-11-2015.
 */
public class customListAdapterldrequirements extends ArrayAdapter<LdNewsItem>{
Context context;
    List<LdNewsItem>objects;
    int layoutResourceId;
    private LayoutInflater layoutInflater;
    public customListAdapterldrequirements(Context context,int layoutresourceid, List<LdNewsItem> listData) {
        super(context,0,listData);
        this.context=context;
        this.layoutResourceId=layoutresourceid;
        objects = listData;
        layoutInflater = LayoutInflater.from(context);
    }




    @Override
    public int getCount() {
        return objects.size();
    }

    public void setObjectValue(List<LdNewsItem> info){
        objects=info;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
LdNewsItem ldnews= objects.get(position);
        View mview = convertView;

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
           convertView = inflater.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: " + ldnews.getSbu());
            holder.reportedDateView.setText("Business Unit: " + ldnews.getSbuu());
            holder.ldstatusView.setText("Sub Business Unit: " + ldnews.getSc());
            holder.ldtype.setText("Service Category: " + ldnews.getSsc());
            holder.ldsource.setText("Service Sub Category: " + ldnews.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;
    }
}

当我尝试添加新的数据列表时,覆盖不动态添加donno在哪里做错了。我希望你们在这里提前帮助谢谢!

0 个答案:

没有答案