简体   繁体   中英

ListView, prevent adding new items

i have my listview activity that works pretty fine but i have an annoying problem. Whenever it resumes it adds the same items in the the list and that ist gets bigger. I just want it to keep the values i feeded at first. How can i do that? here is my activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.custom_list_view);

         adapter = new SimpleAdapter(
                this,
                list,
                R.layout.custom_row_view,
                new String[] {"pen","price","color"},
                new int[] {R.id.text1,R.id.text2, R.id.text3}
                );


        populateList();
        setListAdapter(adapter);
    }

And here is my arrayList:

static final ArrayList<LinkedHashMap<String,String>> list = 
        new ArrayList<LinkedHashMap<String,String>>(); 

    private void populateList() {
        LinkedHashMap<String,String> temp = new LinkedHashMap<String,String>();
        temp.put("pen","                             Mission name");    
        list.add(temp);
        LinkedHashMap<String,String> temp1 = new LinkedHashMap<String,String>();
        temp1.put("pen","                            Map activity");        

        list.add(temp1);
        LinkedHashMap<String,String> temp2 = new LinkedHashMap<String,String>();
        temp2.put("pen","                            Check sensors");       
        list.add(temp2);

        LinkedHashMap<String,String> temp3 = new LinkedHashMap<String,String>();
        temp3.put("pen","                            Infrared Image");
        list.add(temp3);

        LinkedHashMap<String,String> temp4 = new LinkedHashMap<String,String>();
        temp4.put("pen","                            Radar Image");
        list.add(temp4);

        LinkedHashMap<String,String> temp5 = new LinkedHashMap<String,String>();
        temp5.put("pen","                            Visual image");
        list.add(temp5);

        LinkedHashMap<String,String> temp6 = new LinkedHashMap<String,String>();
        temp6.put("pen","                            suscribe to Viewer");
        list.add(temp6);
    }

Don't declare the list as static. ie:

final ArrayList<LinkedHashMap<String,String>> list = new ArrayList<LinkedHashMap<String,String>>(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM