For Loop array is not working in listview in android -



For Loop array is not working in listview in android -

i want display array items in listview when used simple array works , when create array using loop not working. here code

public class mainactivity extends activity { string nm; int number=0; int ttl; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview tv=(textview) findviewbyid(r.id.textview1); listview listview = (listview) findviewbyid(r.id.list); //string[] values =new string[]{"val1","val2"}; contentresolver cr=getcontentresolver(); cursor cur = cr.query(contactscontract.contacts.content_uri, null, null, null, null); ttl=cur.getcount()+1; string[] mystring = new string[ttl]; list<string> values = new arraylist<string>(); if (cur.getcount() > 0) { while (cur.movetonext()) { string id = cur.getstring( cur.getcolumnindex(contactscontract.contacts._id)); string name = cur.getstring( cur.getcolumnindex(contactscontract.contacts.display_name)); if (integer.parseint(cur.getstring(cur.getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) { //query phone here. covered next nm=nm + name; number++; // mystring[number] = "image" + number; values.add(mystring[number]); } } } string[] mystring1 = new string[12]; (int number1 = 0; number1 <= 11; number1++) { mystring1[number1] = "image1" + number1; } arrayadapter<string> adapter=new arrayadapter<string> (this,android.r.layout.simple_list_item_1,android.r.id.text1,values) ; listview.setadapter(adapter); }

replace

values.add(mystring[number]);

by

values.add(nm);

you adding mystring[number] arraylist. mystring doesn't contain elements, empty array size ttl. if had not initialized string[] mystring = new string[ttl] @ values.add(mystring[number]) have got arrayindexoutofbounds exception. , don't need utilize nm=nm + name, unnecessary.

assuming want add together names list. if want add together "image" + number; list uncomment line.

android listview

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -