0

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,

  1. Remove All the customers by clicking ImageView. (problem happens if i remove all the customers. Otherwise it works fine.)

  2. go to add customer tab.

  3. add customer and click save button(Which triggers saveCustomer().)
  4. slide back to view all customer.
  5. No customer added.(even though customer added message shown).

What have i done wrong? Please explain. Thanks.

SajithK
  • 832
  • 10
  • 17

3 Answers3

1

You need to call

adapter.notifyDataSetChanged();

in your fragment in saveCustomer() method.

or you can fire a broadcast using Android LocalBroadCastManager which you can listen in another fragment having adapter in which you call adaper.notifyDataSetChanged() method.

maveroid
  • 1,840
  • 1
  • 20
  • 20
  • yes. I had to call adapter.notifyDataSetChanged(). But first i had to create my CustomerAdapter in my Appcontroller which extends from Application. That maks my CustomerAdapter globally accesible. Thanks maveroid. – SajithK Aug 28 '15 at 06:22
  • And also i found this answer from @Tyler Davis in [this question](http://stackoverflow.com/questions/10073214/viewpager-setoffscreenpagelimit0-doesnt-work-as-expected) is very interesting. I used in CustomerFragment this '@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser){ adapter.notifyDataSetChanged(); } }' – SajithK Aug 28 '15 at 06:43
0

You need to change the referenced list inside the adapter. Add this method to the adapter, and call it when the list changes with the new list as a parameter.

 public void refresh(ArrayList<Customer> customers){        
        this.customers = customers;
        notifyDataSetChanged();
    }
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
  • So i have to register a listener to the list?if so what is the listener? – SajithK Aug 28 '15 at 04:46
  • If you make the change from the adapter, just call refresh(AppController.getInstance().getCustomers(),getActivity()); But if you made the change from somewhere else, for example from a fragment, call adapter.refresh(AppController.getInstance().getCustomers(),getActivity()); – Mikelis Kaneps Aug 28 '15 at 06:40
0

If I got you right, you are showing two fragments in one activity. One for adding a customer One for showing a list of customers Try to debug and check if the newly created customer data really comes, if so ideally you would just need notify the list adapter as adapter.notifyDataSetChanged()

But if its still not reflecting then it may be possible that when you swipe none fragment is actually removed from memory and it shows the old content. Try to recreate the fragment forcefully.

FragmentTransaction tr = getFragmentManager().beginTransaction();
tr.replace(R.id.your_fragment_container, yourFragmentInstance);
tr.commit()
Rohit Jagtap
  • 1,660
  • 1
  • 14
  • 12