Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arraylist<hashmap<>> values repeating in listview

I am trying to get the contents of a Sqlite database into a listview using simple adapter. I am using a arraylist> to get all the values. It is getting displayed on the listview but the values seem to repeat everytime i run it. what should i do? Here's my code

             ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();
              Databasehandler db=new Databasehandler(this);

              private ListView lv;
                  protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
             setContentView(R.layout.contacts);
             val=db.getAllContacts();
                   final ListView lv=(ListView)findViewById(R.id.listView1);


               if(val.size()!=0)
                {
             ListAdapter ad=new SimpleAdapter(Contact.this,val,R.layout.v,new String[]{"contacts"},new int[]{R.id.textView2});

               lv.setAdapter(ad);
           }
like image 882
anu_r Avatar asked Dec 05 '25 04:12

anu_r


1 Answers

Just declare the ArrayList before onCreate method like...

ArrayList<HashMap<String, String>>val = null;

And initialize this ArrayList when you want put retrieved values into the ArrayList like...

val=new ArrayList<HashMap<String,String>>();
like image 188
Hamid Shatu Avatar answered Dec 08 '25 03:12

Hamid Shatu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!