我的疑问是我有两个片段a和b需要在片段a中填充listview从片段b数据中动态获取填充但是一旦我按下后退按钮listview项目丢失不是callilng任何webservice或db我只是想做它内部是我的代码:
片段B(from_fragment):
package first.service.precision.servicefirst;
/**
* Created by 4264 on 23-11-2015.
*/
import android.app.Activity;
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;
datato datatol;
@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);
setRetainInstance(true);
create_acc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newAccountEditTextDetails();
spinnerAdapter();
Accounts accounts;
accounts=(Accounts)getFragmentManager().findFragmentByTag("accounts");
accounts.updatelistview(objnewacntview);
setRetainInstance(true);
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());
}
public void sendData(String sk,String s){
datatol.data(sk,s);
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
datatol = (datato) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ "");
}
}
public interface datato{
void data(String str,String sl);
}
}
这是我的片段a:
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);
lv = (ListView) rootView.findViewById(R.id.lvaccounts);
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();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.content_frame, newFragment, "newaccount");
transaction.addToBackStack(null);
transaction.commit();
}
}
@Override
public void onDestroyView(){
super.onDestroyView();
}
public void datarecieve(String str, String sl) {
NewAccountView newAccountView=new NewAccountView();
newAccountView.setCompany_grp(str);
newAccountView.setCompnay_name(sl);
}
}
我的疑问是,即使我按下我需要保留我的列表视图如何才能完成此事先谢谢!