ListView,阻止添加新项目

时间:2012-02-28 17:36:36

标签: android

我的listview活动工作得很好,但我有一个恼人的问题。 每当它恢复时,它会在列表中添加相同的项目,并且它会变得更大。我只想让它保持我最初的价值。 我怎样才能做到这一点? 这是我的活动

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);
    }

这是我的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);
    }

1 个答案:

答案 0 :(得分:1)

不要将列表声明为静态。 即:

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