1

Hi all i'm new in Android. I'm developing app that contains ListAdapter, and i want to clear it from onResume.

in Activity refers to levelsView.

public class MyActivity extends Activity{
                  ...


@Override
    protected void onResume(){
        super.onResume();
        levelsView.onResume();
    }
}

LevelsView refers to FontsSelect where I have myAdapter

public class FontsSelect extends LinearLayout {
private List<FontLevels> fontsLevels;

public FontsSelect(Context context, AttributeSet attr, int defStyle){
        super(context, attr, defStyle);
        inflate(context, R.layout.list_view_of_fonts, this);

        listViewFonts = (ListView)findViewById(R.id.list_of_fonts);
        listViewFonts.setDivider(null);
        fontsLevels = new ArrayList<FontLevels>();
        levels = new ArrayList<Level>();
        createLevels();
        myAdapter = new MyAdapter(context);
        listViewFonts.setOnItemClickListener(levelSelectListener);
        listViewFonts.setAdapter(myAdapter);

    }

 public void createLevels(){
    ...
 fontsLevels.add(new FontLevels(context,  "fonts/TNR.ttf"))}

class  MyAdapter extends BaseAdapter {

        final LayoutInflater inflater;

        public MyAdapter(Context context){
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return fontsLevels.size();
        }

        @Override
        public Object getItem(int position) {
            return fontsLevels.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;  
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if(convertView == null){
                convertView = inflater.inflate(R.layout.item_level, parent, false);
                holder = new ViewHolder((TextView)convertView.findViewById(R.id.level_name_typeface), (TextView)convertView.findViewById(R.id.level_score_number));
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder)convertView.getTag();
            }

            ...


            return convertView;
        }



    }

public void onResume(){
        listViewFonts.setAdapter(null);
        fontsLevels.clear();
        fontsLevels.add(new FontLevels(context,  "fonts/TNR.ttf");
        fontsLevels.add(new FontLevels(context,  "fonts/TNR.ttf");
        myAdapter = new MyAdapter(getContext(), fontsLevels);
        listViewFonts.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();

    }
}

Here is the ckass FontLevel

public class FontLevels{
private String fontFullName;
    private int _id;
    private List<Level> easyLevels;
    private List<Level> hardLevels;

public FontLevels(Context context, String fontFullName){
        this.fontFullName = fontFullName;
        typeface = Typeface.createFromAsset(context.getAssets(), FONTS_DIR + fontFullName);
        easyLevels =  new ArrayList<Level>(levelNumber);
        hardLevels = new ArrayList<Level>(levelNumber);
}

And after restart my app, adapter doesn't update, and show old list. Please help)) Thanx!!!

Ihor Bykov
  • 1,843
  • 3
  • 15
  • 22
  • Are you sure your array list is being updated when you are restarting the app? If not then its the general behavior to show old values :) – dinesh sharma Aug 06 '13 at 14:49
  • i debuged app, and see that, after restart, constructor are initialize array(`createLevels()`) and `getCount()` return right value, but then `onResume` clear array and `getCount() = 0`, and finally, i don't understand how, getCount() return old value, given the fact that nobody initialize array – Ihor Bykov Aug 06 '13 at 14:59
  • Actually you have made customized class and you have hide some of your code not able to get the flow and mappings. can you post atleast methods with their params and response. – dinesh sharma Aug 06 '13 at 15:03

3 Answers3

0

This post point out the problem and describe all the possible solution

Community
  • 1
  • 1
0

notifydatasetchanged() only updates the view if the adapter.add()/adapter.clear()/adapter.remove() methods are used to modify the underlying data structure being presented by the adapter.

If you are modifying the underlying data structure directly, you need to reinstantiate the adapter passing the new data structure and re-set the view adapter.

I found this problem in my own code and reinstantiating the adapter fixed it.

-- Update -- Try adding

myAdapter = new MyAdapter();
listViewFonts.setAdapter(myAdapter);

to onResume() to recreate a "new" instance of the adapter and set it for the listview

rcbevans
  • 7,101
  • 4
  • 30
  • 46
  • Thank you, i'll try it. But i have a question, if i add button and set it clickListener where `.clear()` my array, and thren `adapter.notifydatasetchanged()` view will changed and it's ok. But why can't do the same from `onResume()`. Thank you! – Ihor Bykov Aug 06 '13 at 15:48
  • I'll be honest, I'm not 100% sure, but I think the scope of the listview objects is lost when you go through onPause in preparation for the potential of application being closed though low memory, hence when you come back into onResume, the array is no longer directly linked to the array adapter objects. So notify doesn't work. Again, a guess, all I can say is I have observed the same behaviour and this is how I fixed it – rcbevans Aug 06 '13 at 15:54
  • Can I ask you a little favor, and write a small example of how you implemented it. I would be very grateful to you :) – Ihor Bykov Aug 07 '13 at 05:51
  • I added new `onResume()` to my app, but it doesn't work. Maybe I don't understand what you say :( – Ihor Bykov Aug 07 '13 at 06:00
  • I did exactly what you said( you can see it in my code) but it doesn't work :( – Ihor Bykov Aug 07 '13 at 15:29
  • I've made a terrible mistake, I'm ashamed to write it here ... sorry that I wasteon it day and a half, but your answer is correct – Ihor Bykov Aug 07 '13 at 17:27
-1

The following cleared the ListView for me:

        listview.removeAllViewsInLayout();
        listview.removeAllViewsFromAdapter();
        listview.setAdapter(null);
        listview.refreshDrawableState();
        listview.removeViews(0, listview.getChildCount());
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • I added it, but it doesn't work :( `@Override protected void onResume(){ super.onResume(); levelsView.fontsSelect.listViewFonts.removeAllViewsInLayout(); levelsView.fontsSelect.listViewFonts.setAdapter(null); levelsView.fontsSelect.listViewFonts.refreshDrawableState(); levelsView.fontsSelect.listViewFonts.removeViews(0, levelsView.fontsSelect.listViewFonts.getChildCount()); }` – Ihor Bykov Aug 06 '13 at 15:30
  • Manually clearing views isn't the way to do it, the adapter will detect the views are altered and manually refresh itself, putting them straight back – rcbevans Aug 06 '13 at 15:44