varunrao321
varunrao321

Reputation: 965

repeating ListView

Hey guys I am a working on displaying a list of items on listview, the code I am using is

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. 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. I am not able to figure out how to solve this.

Upvotes: 0

Views: 223

Answers (2)

Jonas
Jonas

Reputation: 2126

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. So, whenever the activity is created, additional data is populated.

Upvotes: 1

Android
Android

Reputation: 9033

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?
What adapter shall I use to use HashMap in a ListView

Upvotes: 0

Related Questions