0

SimpleAdapterを表示するために使用しListViewていますが、戻るボタンを押してアクティビティ(リストを表示)を再度開くと、リストのエントリが2倍になります。

もう一度行うと、配列値はリストの項目と再び連結されます。

配列のリスト変数は次のように宣言されています

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

onCreate()の私のコードList.java

setContentView(R.layout.list);



    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("first","Strength");
    temp.put("second", strength);
    list.add(temp);


    HashMap<String,String> temp1 = new HashMap<String,String>();
    temp1.put("first","what");
    temp1.put("second", "??");
    list.add(temp1);


    HashMap<String,String> temp2 = new HashMap<String,String>();
    temp2.put("first","Time");
    temp2.put("second", "time");
    list.add(temp2);


    HashMap<String,String> temp3 = new HashMap<String,String>();
    temp3.put("first","Repeat");
    temp3.put("second", "everyday");
    list.add(temp3);




    setListAdapter(new SimpleAdapter(this,list,R.layout.row_view, new String [] {"first","second"}, new int [] {R.id.rowTextView1, R.id.rowTextView2} ));
4

1 に答える 1

1

次のように、ifステートメントを作成して、リストに既に何かがあるかどうかを確認できます。

if(list.size() == 0){

HashMap<String,String> temp = new HashMap<String,String>();
temp.put("first","Strength");
temp.put("second", strength);
list.add(temp);


HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("first","what");
temp1.put("second", "??");
list.add(temp1);


HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("first","Time");
temp2.put("second", "time");
list.add(temp2);


HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("first","Repeat");
temp3.put("second", "everyday");
list.add(temp3);
}
于 2012-07-11T21:47:01.093 に答える