在碎片中按下后,Listview项目被删除了吗?

时间:2015-12-11 12:32:06

标签: android listview android-fragments android-listview

这是我的概率我有两个片段a和b。片段a中的listview从片段b填充。但是当从片段中按下时,列表项会被删除。这个问题很疯狂:

这是我的片段B(From_fragment):

package first.service.precision.servicefirst;

/**
 * Created by 4264 on 23-11-2015.
 */

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 18-11-2015.
 */
public class NewAccount extends Fragment {
    EditText compny_grp,compny_name,address_line,pin,landline,weburl,email;
    Spinner spinner_state,spinner_city,spinner_industry,spinner_account_manager,spinner_region;
  Button create_acc;
    NewAccountView objnewacntview=new NewAccountView();
    SpinnerAccount spinnerAccount;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_new_account, container,false);
       compny_grp=(EditText)view.findViewById(R.id.cmpnygrp);
        compny_name=(EditText)view.findViewById(R.id.cmpnyname);
        address_line=(EditText)view.findViewById(R.id.addrs1);
        spinner_state=(Spinner)view.findViewById(R.id.state);
        spinner_city=(Spinner)view.findViewById(R.id.city);
         pin=(EditText)view.findViewById(R.id.pin);
         landline=(EditText)view.findViewById(R.id.ll_1);
         weburl=(EditText)view.findViewById(R.id.url);
         email=(EditText)view.findViewById(R.id.email);
         spinner_industry=(Spinner)view.findViewById(R.id.indstry);
         spinner_account_manager=(Spinner)view.findViewById(R.id.acm);
        spinner_region=(Spinner)view.findViewById(R.id.region);
create_acc=(Button)view.findViewById(R.id.create_account);

        create_acc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newAccountEditTextDetails();
                spinnerAdapter();
                Accounts accounts;
                accounts=(Accounts)getFragmentManager().findFragmentByTag("accounts");
                accounts.updatelistview(objnewacntview);
                getActivity().getFragmentManager().popBackStack();
            }
        });
        return view;
    }

    public void newAccountEditTextDetails(){

        objnewacntview.setCompany_grp(compny_grp.getText().toString());
        objnewacntview.setCompnay_name(compny_name.getText().toString());
        objnewacntview.setAddrss_line(address_line.getText().toString());
        objnewacntview.setPin(pin.getText().toString());
        objnewacntview.setLandline(landline.getText().toString());
        objnewacntview.setWeburl(weburl.getText().toString());
        objnewacntview.setEmail(email.getText().toString());

    }
    public void spinnerAdapter(){
        List<SpinnerAccount>state=new ArrayList<SpinnerAccount>();
        state.add(new SpinnerAccount("Tamilnadu",1));
        state.add(new SpinnerAccount("Maharashtra",2));
        state.add(new SpinnerAccount("Kerala",3));
        state.add(new SpinnerAccount("Karnataka",4));
        state.add(new SpinnerAccount("Andhra Pradesh",5));
        List<SpinnerAccount>city=new ArrayList<SpinnerAccount>();
        city.add(new SpinnerAccount("Chennai",1));
        city.add(new SpinnerAccount("Coimbatore",2));
        city.add(new SpinnerAccount("Trichy",3));
        city.add(new SpinnerAccount("Tirunelvelli",4));
        city.add(new SpinnerAccount("Madurai",5));
        List<SpinnerAccount>industry=new ArrayList<SpinnerAccount>();
        industry.add(new SpinnerAccount("Asp",1));
        industry.add(new SpinnerAccount("Erp",2));
        industry.add(new SpinnerAccount("Msp",3));
        List<SpinnerAccount>region=new ArrayList<SpinnerAccount>();
        region.add(new SpinnerAccount("Sounth",1));
        region.add(new SpinnerAccount("North",2));
        region.add(new SpinnerAccount("west", 3));
        region.add(new SpinnerAccount("East", 4));
        List<SpinnerAccount>accountmanagers=new ArrayList<SpinnerAccount>();
        accountmanagers.add(new SpinnerAccount("Matthew",1));
        //setting adapter
        AccountSpinnerAdapter stateadapter=new AccountSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,state);
        spinner_state.setAdapter(stateadapter);
        AccountSpinnerAdapter cityadapter=new AccountSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,city);
        spinner_city.setAdapter(cityadapter);
        AccountSpinnerAdapter industryadapter=new AccountSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,industry);
        spinner_industry.setAdapter(industryadapter);
        AccountSpinnerAdapter regionadapter=new AccountSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,region);
        spinner_region.setAdapter(regionadapter);
        AccountSpinnerAdapter accountmanager=new AccountSpinnerAdapter(getActivity().getBaseContext(),R.layout.spinnerlayout,accountmanagers);
       spinner_account_manager.setAdapter(accountmanager);
        //picking values
        spinnerAccount=(SpinnerAccount)spinner_state.getSelectedItem();
        objnewacntview.setState(spinnerAccount.getText());
        objnewacntview.setStateid(spinnerAccount.getValue());
        spinnerAccount=(SpinnerAccount)spinner_city.getSelectedItem();
        objnewacntview.setCity(spinnerAccount.getText());
        objnewacntview.setCityid(spinnerAccount.getValue());
        spinnerAccount=(SpinnerAccount)spinner_account_manager.getSelectedItem();
        objnewacntview.setAccount_manager(spinnerAccount.getText());
        objnewacntview.setAccountmanager(spinnerAccount.getValue());
        spinnerAccount=(SpinnerAccount)spinner_region.getSelectedItem();
        objnewacntview.setRegion(spinnerAccount.getText());
        objnewacntview.setRegionid(spinnerAccount.getValue());
        spinnerAccount=(SpinnerAccount)spinner_industry.getSelectedItem();
        objnewacntview.setIndustry(spinnerAccount.getText());
        objnewacntview.setIndustryid(spinnerAccount.getValue());


    }



}

这是片段a(To_fragment)

package first.service.precision.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.Button;
import android.widget.ListView;

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

/**
 * Created by 4264 on 08-10-2015.
 */
public class Accounts extends Fragment implements View.OnClickListener {
    public Accounts(){}
public ListView lv;
    public AccountListAdapter accountListAdapter;
    List<NewAccountView>newAccountViews;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);



     //   ArrayList<NewsItem> listContact = GetlistContact();
        lv = (ListView) rootView.findViewById(R.id.lvaccounts);
     //   lv.setAdapter(new CustomListAdapter(getActivity(), listContact));
newAccountViews=new ArrayList<NewAccountView>();
        accountListAdapter=new AccountListAdapter(getActivity(),R.id.lvaccounts,newAccountViews);
        lv.setAdapter(accountListAdapter);

        return rootView;

    }


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

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


    }
    public void updatelistview(NewAccountView obj){
        newAccountViews.add(obj);

        accountListAdapter.notifyDataSetChanged();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.New_Account:
                Fragment newFragment = new NewAccount();

                // consider using Java coding conventions (upper first char class names!!!)
                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                transaction.add(R.id.content_frame, newFragment, "newaccount");
                transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();


        }
    }}

我的问题是,当我从片段按回按钮时,listview项目被删除我怎么能解决这个希望有人可以解决这个问题!

0 个答案:

没有答案