简体   繁体   English

重复列表视图

[英]repeating ListView

Hey guys I am a working on displaying a list of items on listview , the code I am using is嘿伙计们,我正在努力在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);
    }

problem is say I am displaying three names Avinash, Arun, Rajesh.问题是说我正在显示三个名字 Avinash、Arun、Rajesh。 When application starts these three names are displayed on list.当应用程序启动时,这三个名称显示在列表中。 When I close and again start the application the values are repeating Avinash, Arun, Rajesh,Avinash, Arun, Rajesh.当我关闭并再次启动应用程序时,值会重复 Avinash、Arun、Rajesh、Avinash、Arun、Rajesh。 I am not able to figure out how to solve this.我无法弄清楚如何解决这个问题。

The code you show seems fine.您显示的代码似乎很好。 My guess is that listclass.populate() modifies offersobj and that offersobj is reused over several creations of your activity.我的猜测是listclass.populate()修改offersobj并且offersobj在您的活动的多个创建中被重用。 So, whenever the activity is created, additional data is populated.因此,无论何时创建活动,都会填充其他数据。

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

for more example see this link有关更多示例, 请参见此链接
this also 这也是
listview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview? 关于适配器的列表视图,用于创建 hashmap,为什么导入的 bitmap 类型无法在列表视图中显示图像?
What adapter shall I use to use HashMap in a ListView 我应该使用什么适配器在 ListView 中使用 HashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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