Monday 15 September 2014

java - Subclass ArrayAdapter in android to display ArrayList -



java - Subclass ArrayAdapter in android to display ArrayList -

i asked similar question here , 1 user suggested subclass arrayadapter, have issues that. here problem:

i utilize hashmap<string, string> , simpleadapter object display user info in listview, want replace hashmap<string, string> arraylist<user>(); containing real user objects.

here old code:

hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_username, c.getstring(tag_username)); map.put(tag_firstname, c.getstring(tag_firstname)); map.put(tag_lastname, c.getstring(tag_lastname)); map.put(tag_address, c.getstring(tag_address)); userslist.add(map);

[...]

listadapter adapter = new simpleadapter(this, userslist, r.layout.single_user, new string[] { tag_username, tag_firstname, tag_lastname, tag_address }, new int[] { r.id.textviewusername, r.id.textviewfirstname, r.id.textviewlastname, r.id.textviewaddress });

now, suggested, tried subclass arrayadapter, have no plan how getview(int, view, viewgroup) should like, accomplish objective.

here current code:

public class userarrayadapter extends arrayadapter<user> { private context mcon; private list<user> userlist; public userarrayadapter(context context, int resource, list<user> userlist) { super(context, resource); mcon = context; this.userlist = userlist; } @override public view getview(int position, view convertview, viewgroup parent) { (user usr : userlist) { findviewbyid(r.id.textviewusername).settext(usr.getfirstname()); } homecoming super.getview(position, convertview, parent); } }

here user class:

public class user { private string username, firstname, lastname, address; public user(string username, string firstname, string lastname, string address) { setusername(username); setfirstname(firstname); setlastname(lastname); setaddress(address); } public string getusername() { homecoming username; } public void setusername(string username) { if (username == null) { throw new illegalargumentexception("username null."); } this.username = username; } public string getfirstname() { homecoming firstname; } public void setfirstname(string firstname) { if (firstname == null) { throw new illegalargumentexception("firstname null."); } this.firstname = firstname; } public string getlastname() { homecoming lastname; } public void setlastname(string lastname) { if (lastname == null) { throw new illegalargumentexception("lastname null."); } this.lastname = lastname; } public string getaddress() { homecoming address; } public void setaddress(string address) { if (address == null) { throw new illegalargumentexception("address null."); } this.address = address; } }

and thought phone call this:

listadapter advertisement = new userarrayadapter(this, r.layout.single_user, usersarrlist);

any help appreciated.

i'd suggest bypassing arrayadapter altogether as, since you're going doing custom work, you're going duplicating effort done adapter internally. implementation trivial anyway. subclass baseadapter:

public class useradapter extends baseadapter { private final list<user> musers; private final layoutinflater minflater; public useradapter(context ctx, collection<user> users) { musers = new arraylist<user>(); minflater = layoutinflater.from(ctx); if (users != null) { musers.addall(users); } } @override public int getcount() { // homecoming number of users in info set homecoming musers.size(); } @override public long getitemid(int pos) { // irrelevant if you're not using cursors homecoming pos; } @override public user getitem(int pos) { // homecoming item @ specified position homecoming musers.get(pos); } @override public view getview(int pos, view convertview, viewgroup parent) { userviewholder uvh; if (convertview == null) { // inflate layout if there's not recycled view convertview = minflater.inflate(r.layout.single_user, parent, false); // tag view class holding views found // findviewbyid() prevent lookups later uvh = new userviewholder(convertview); convertview.settag(uvh); } else { // if view non-null, have // created view holder , set tag uvh = (userviewholder) convertview.gettag(); } // user @ specified position , // set view necessary user user = getitem(pos); uvh.usernametxt.settext(user.getusername()); homecoming convertview; } public static class userviewholder { public final textview usernametxt; public userviewholder(view v) { usernametxt = (textview) v.findviewbyid(r.id.textviewusername); } } }

java android arraylist

No comments:

Post a Comment