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);
}
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>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With