Search code examples
androidandroid-listfragment

ListFragment overlap on update


I try to refresh my CustomListFragment which extends from ListFragmentin a two pane layout after clicking on a NavigationDrawer with:

public void updateInputView(String date)
{
    ArrayList<Custom> values;
    DataSource ds = new DataSource(this.getActivity());

    ds.openReadOnly();
    values = ds.getValues(date);
    ds.close();

    CustomAdapter adapter = new CustomAdapter(this.getActivity(), values);

    adapter.notifyDataSetChanged();
    setListAdapter(adapter);
}

After calling updateInputView the second list overlaps the first list, which was created during startup. Now when I choose another one in the NavigationDrawer, the second list disappears and the third list comes up overlapping the first list again.

So what is wrong here?

Edit:

private void createView()
{
    String  date;

    if (sDate == null)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        date = dateFormat.format(cal.getTime());
    }
    else
    {
        date=sDate;
    }

    DataSource ds = new DataSource(this.getActivity());

    ds.openReadOnly();
    values = ds.getValues(date);
    ds.close();

    updateInputView(date);

}

This was the code before.

After:

public void updateInputView(String date)
{


    DataSource ds = new DataSource(this.getActivity());

    ds.openReadOnly();
    values = ds.getValues(date);
    ds.close();


    this.getActivity().setTitle(this.getResources().getString(R.string.app_name)+" "+ds.toPrettyDate(date));
    adapter.notifyDataSetChanged();

    //setListAdapter(adapter);
}


private void createView()
{
    String  date;

    if (sDate == null)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        date = dateFormat.format(cal.getTime());
    }
    else
    {
        date=sDate;
    }

    DataSource ds = new DataSource(this.getActivity());

    ds.openReadOnly();
    values = ds.getValues(date);
    ds.close();

    this.getActivity().setTitle(this.getResources().getString(R.string.app_name)+" "+ds.toPrettyDate(date));    
adapter = new CustomAdapter(this.getActivity(), values);
    setListAdapter(adapter);

}

Solution

  • You need to call setListAdapter(adapter); only once. For updating the list just call adapter.notifyDataSetChanged();

    public class Mainactivity extends ListFragment {
        CustomAdapter adapter ;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
           // Your code .....
           if(adapter ==null){
               adapter = new CustomAdapter(this.getActivity(), values);
               setListAdapter(adapter);
           } 
        }
    
    
        public void updateInputView(String date)
        {
            ArrayList<Custom> values;
            DataSource ds = new DataSource(this.getActivity());
    
            ds.openReadOnly();
            values = ds.getValues(date);
            ds.close();
    
            if(adapter!=null){
                adapter.notifyDataSetChanged();
          }
       }
    }
    

    P.S : Make sure onCreateView is called only once.

    Hope it will help you ツ