android - SetAdapter only works on the first element of String[] -
i new android programming , have been stuck @ problem week. there many similar threads out there have accomplished i'm trying do, they're not want. or @ least i'm unable understand they're saying.
i've got file list of groceries, 1 on each line. format is:
true grocery name true grocery false third grocery
i reading file in main activity , have built xml fixed header , footer , scrollable dynamic middle layer in xml listview
checkbox
inside of it. have built (actually, google'd) adapter read file in array of strings , further apply first word (true/ false) checkbox
, rest of line checkbox text. unfortunately working first item in list.
this working when dint have header , main class extending listactivity. since wanted more buttons, changed action bar , since i'm unable use setlistadapter
, , forced use setadapter
instead. can tell me why acts upon first line , not all.
i have hunch setadapter instead of setlistadapter might messing up. i'm not sure. please let me know if need see xml structure.
my code follows:
file sdcard = environment.getexternalstoragedirectory(); file grocerylist = new file(sdcard, "grocery.txt" ); // method read data file private void readfile() { stringbuilder text = new stringbuilder(); try { bufferedreader br = new bufferedreader(new filereader(grocerylist)); string line; arraylist<string> item = new arraylist<string>(); while ((line = br.readline()) != null) { item.add(line); } string[] values = item.toarray(new string[item.size()]); br.close(); listview list = (listview)findviewbyid(r.id.container); list.setadapter(new grocerylistadapter(this,values)); //list.setadapter(new arrayadapter<string>(this, r.layout.checkbox, r.id.label, values)); } catch (ioexception e) { toastmsg("read failed!! " + e.getmessage()); } }
this adapter:
package in.hardikar.apps.grocerylist; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.toast; public class grocerylistadapter extends arrayadapter<string> { private final context context; private final string[] values; public grocerylistadapter(context context, string[] values) { super(context, r.layout.checkbox, values); this.context = context; //confirmed entire file loading in string[] values this.values = values; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.checkbox, parent, false); // inflate content container //textview textview = (textview) rowview.findviewbyid(r.id.label); //imageview imageview = (imageview) rowview.findviewbyid(r.id.logo); checkbox checkbox = (checkbox) rowview.findviewbyid(r.id.label); toast.maketext(context, values[position] + " loaded", toast.length_short).show(); //textview.settext(values[position]); string line = values[position]; string data[] = line.split(" ",2); checkbox.settext(data[1]); if(data[0].equalsignorecase("true")) { checkbox.setchecked(true); } else { checkbox.setchecked(false); } //inflater.inflate(r.layout.checkbox, list); return rowview; } }
found answer here
you cant have list view inside of scroll view. listview can take care of scrolling itself.. wohooo
the current documentation says: "you should never use scrollview listview, because listview takes care of own vertical scrolling. importantly, doing defeats of important optimizations in listview dealing large lists, since forces listview display entire list of items fill infinite container supplied scrollview."
Comments
Post a Comment