簡體   English   中英

重復列表視圖

[英]repeating ListView

嘿伙計們,我正在努力在listview上顯示項目列表,我正在使用的代碼是

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_list_view);
        ListView lv= (ListView)findViewById(R.id.listview);
        rtrnList = new ArrayList<HashMap<String,String>>();
        getmyLocation();
        listclass = new listClass(offersobj);
        listclass.populate();
        rtrnList = listclass.getListArray();
        adapter = new SimpleAdapter(
                this,
                rtrnList,
                R.layout.custom_row_view,
                new String[] {"Name","Msg","time"},
                new int[] {R.id.text1,R.id.text2, R.id.text3}
                );

        lv.setAdapter(adapter);
    }

問題是說我正在顯示三個名字 Avinash、Arun、Rajesh。 當應用程序啟動時,這三個名稱顯示在列表中。 當我關閉並再次啟動應用程序時,值會重復 Avinash、Arun、Rajesh、Avinash、Arun、Rajesh。 我無法弄清楚如何解決這個問題。

您顯示的代碼似乎很好。 我的猜測是listclass.populate()修改offersobj並且offersobj在您的活動的多個創建中被重用。 因此,無論何時創建活動,都會填充其他數據。

public class ListViewA extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView lv= (ListView)findViewById(R.id.listview);

        // create the grid item mapping
        String[] from = new String[] {"rowid", "col_1", "col_2", "col_3"};
        int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };

        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i < 10; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("rowid", "" + i);
            map.put("col_1", "col_1_item_" + i);
            map.put("col_2", "col_2_item_" + i);
            map.put("col_3", "col_3_item_" + i);
            fillMaps.add(map);
        }

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
        lv.setAdapter(adapter);
    }
}

有關更多示例, 請參見此鏈接
這也是
關於適配器的列表視圖,用於創建 hashmap,為什么導入的 bitmap 類型無法在列表視圖中顯示圖像?
我應該使用什么適配器在 ListView 中使用 HashMap

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM