I have a activity with two tabs. Tabs are created with Custom Adpater which extends FragmentStatePagerAdapter.
public CustomerScrollAdpater(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
addCustomer=new AddCustomerFragment();
editCustomer=new CustomerFragment();
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
return addCustomer;
}
else{ // else if the position is 1 we are returning the second tab
return editCustomer;
}
}
AddCustomerFragment has following method attached to button which update a common arraylist(This arraylist is accessible to any activity or fragment).
public void saveCustomer(){
Customer customer = new Customer();
customer.setNic("2222222222V");
AppController.getInstance().getCustomers().add(customer);
Toast.makeText(getActivity(),"Customer successfully added",Toast.LENGTH_SHORT).show();
}
Changes in the arraylist will show in next tab- CustomerFragment.Some Code in the CustomerFragment,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_customer_list, container, false);
listView = (ListView)view.findViewById(R.id.customerListView);
adapter = new CustomerListAdapter(AppController.getInstance().getCustomers(),getActivity());
listView.setAdapter(adapter);
return view;
}
@Override
public void onStart() {
super.onStart();
Toast.makeText(getActivity(),"Started again",Toast.LENGTH_SHORT).show();
}
Here is my custom adaper.
public class CustomerListAdapter extends BaseAdapter {
private ArrayList<Customer> customers;
private Context context;
public CustomerListAdapter(ArrayList<Customer> customers,Context context){
super();
this.customers = customers;
this.context = context;
}
@Override
public int getCount() {
return customers.size();
}
@Override
public Customer getItem(int position) {
return (null == customers)? null : customers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int cusPosition = position;
LayoutInflater layoutInflater = LayoutInflater.from(context);
final View theView = layoutInflater.inflate(R.layout.customer_list_item, parent, false);
TextView textView = (TextView) theView.findViewById(R.id.textCustomerNic);
textView.setText(customers.get(cusPosition).getNic());
ImageView imageView = (ImageView) theView.findViewById(R.id.deleteCustomer);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppController.getInstance().getCustomers().remove(customers.get(cusPosition));
notifyDataSetChanged();
}
});
return theView;
}
}
This works fine. I can delete customers and add customers.
The problem happens when i,
Remove All the customers by clicking ImageView. (problem happens if i remove all the customers. Otherwise it works fine.)
go to add customer tab.
- add customer and click save button(Which triggers saveCustomer().)
- slide back to view all customer.
- No customer added.(even though customer added message shown).
What have i done wrong? Please explain. Thanks.