即使在android片段中按下后,如何保留listview项目?

时间:2015-12-14 05:26:37

标签: android listview android-fragments android-listview

我的疑问是我需要保留listview项目,即使在我的片段中按下后让我解释我的完整概率我有片段名称frag" a"附加到该片段内的活动的另一个片段叫做frag" b" 。需要在片段a中动态填充listview来自片段b数据动态我可以在frag a中填充listview但是当我从frag中按回来时,所有缺少的列表项目不知道该做什么是我的代码:

片段B(From_frag):

package first.service.precision.servicefirst;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 4264 on 20-11-2015.
 */
public class NewContacts extends Fragment {
    EditText first_name,jobtitle,bussiness_phone,email,mobile,address_one,postal_code;
    Spinner gender_spinner,method_of_contact,state,city1;
    ContactView contactView=new ContactView();
    ContactSpinnerItem contactSpinnerItem;
ArrayList<ContactView>contactViews;
    Button save;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_new_contacts, container, false);
        first_name=(EditText)rootView.findViewById(R.id.first_name);
        jobtitle=(EditText)rootView.findViewById(R.id.jobtitle);
        bussiness_phone=(EditText)rootView.findViewById(R.id.businsphn);
        email=(EditText)rootView.findViewById(R.id.email_contact);
        mobile=(EditText)rootView.findViewById(R.id.mobnum);
        address_one=(EditText)rootView.findViewById(R.id.addrss1_contact);
        postal_code=(EditText)rootView.findViewById(R.id.postal_contact);
        //spinners
        gender_spinner=(Spinner)rootView.findViewById(R.id.gender);
        method_of_contact=(Spinner)rootView.findViewById(R.id.cntct_contact);
        state=(Spinner)rootView.findViewById(R.id.state_contact);
        city1=(Spinner)rootView.findViewById(R.id.city_contact);
        save =(Button)rootView.findViewById(R.id.btncontactsave);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contact_textbox();
                contact_spinner();
                Contactss contactss;
                contactss=(Contactss)getFragmentManager().findFragmentByTag("contact");
         //   contactViews=new ArrayList<ContactView>();

             contactss.updatelist(contactView);
                getActivity().getFragmentManager().popBackStack();
            }
        });
        return rootView;

    }



    public void contact_textbox(){
        contactView.setFirst_name(first_name.getText().toString());
        contactView.setJob_title(jobtitle.getText().toString());
        contactView.setBusiness_phone(bussiness_phone.getText().toString());
        contactView.setEmail(email.getText().toString());
        contactView.setMobile(mobile.getText().toString());
        contactView.setAddrs_1(address_one.getText().toString());
        contactView.setPostal_code(postal_code.getText().toString());
    }
    public void contact_spinner(){
        List<ContactSpinnerItem>gender=new ArrayList<ContactSpinnerItem>();
        gender.add(new ContactSpinnerItem("Male",1));
        gender.add(new ContactSpinnerItem("Female",2));
        List<ContactSpinnerItem>methodofcontact=new ArrayList<ContactSpinnerItem>();
        methodofcontact.add(new ContactSpinnerItem("Fax",1));
        methodofcontact.add(new ContactSpinnerItem("Phone",2));
        methodofcontact.add(new ContactSpinnerItem("Email",3));
        methodofcontact.add(new ContactSpinnerItem("Other",4));
        List<ContactSpinnerItem>state_spinner=new ArrayList<ContactSpinnerItem>();
        state_spinner.add(new ContactSpinnerItem("TamilNadu",1));
        state_spinner.add(new ContactSpinnerItem("Maharashtry",2));
        state_spinner.add(new ContactSpinnerItem("Karnataka",3));
        List<ContactSpinnerItem>city=new ArrayList<ContactSpinnerItem>();
        city.add(new ContactSpinnerItem("Chennai",1));
        city.add(new ContactSpinnerItem("Banglore",2));
        city.add(new ContactSpinnerItem("Mumbai", 3));
//adapter for spinner

        ContactSpinnerAdapter genderadapter=new ContactSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,gender);
        gender_spinner.setAdapter(genderadapter);
        ContactSpinnerAdapter method=new ContactSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,methodofcontact);
        method_of_contact.setAdapter(method);
        ContactSpinnerAdapter state1=new ContactSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,state_spinner);
        state.setAdapter(state1);
        ContactSpinnerAdapter citys=new ContactSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,city);
        city1.setAdapter(citys);
        contactSpinnerItem=(ContactSpinnerItem)gender_spinner.getSelectedItem();
        contactView.setGender(contactSpinnerItem.getContact_text());
        contactView.setGender_id(contactSpinnerItem.getContact_value());
        //method of contanct
        contactSpinnerItem=(ContactSpinnerItem)method_of_contact.getSelectedItem();
        contactView.setMethod_of_contact(contactSpinnerItem.getContact_text());
        contactView.setMethod_id(contactSpinnerItem.getContact_value());
       //state
        contactSpinnerItem=(ContactSpinnerItem)state.getSelectedItem();
        contactView.setState_conatct(contactSpinnerItem.getContact_text());
        contactView.setState_id(contactSpinnerItem.getContact_value());
        //city
        contactSpinnerItem=(ContactSpinnerItem)city1.getSelectedItem();
        contactView.setCity(contactSpinnerItem.getContact_text());
        contactView.setCity_id(contactSpinnerItem.getContact_value());

    }

}

这是我的片段a(To_fragment):

package first.service.precision.servicefirst;

/**
 * Created by 4264 on 23-11-2015.
 */
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.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 4264 on 08-10-2015.
 */
public class Contactss extends android.app.Fragment implements View.OnClickListener {
    public Contactss(){}
    public ListView lv;
    Contactss contactss;
    public ContactListAdapter contactListAdapter;
    List<ContactView> newContactViews;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
        lv = (ListView) rootView.findViewById(R.id.lvcontacts);
        //ArrayList<NewsItem> listContact = GetlistContact();

            newContactViews = new ArrayList<ContactView>();
            contactListAdapter = new ContactListAdapter(getActivity(), R.id.lvcontacts, newContactViews);
//contactss= (Contactss) getFragmentManager().getFragment(savedInstanceState,"contact");
            lv.setAdapter(contactListAdapter);


        return rootView;  }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //getFragmentManager().putFragment(outState,"contact",contactss);

    }

    public void updatelist(ContactView obj) {

        newContactViews.add(obj);
        contactListAdapter.notifyDataSetChanged();

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

        Fragment fragment=null;
        Button add = (Button) getActivity().findViewById(R.id.button2);
        add.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        Fragment newfragmnet=new NewContacts();
        FragmentTransaction ft=getFragmentManager().beginTransaction();
        ft.add(R.id.content_frame,newfragmnet);
        ft.addToBackStack(null);
        ft.commit();
    }


}

如果保留列表项目,即使背面被压缩也可以帮助我!

0 个答案:

没有答案