java - put listView child in a hashmap -
i'm trying fetch values listview loop , put them in hashmap. logcat keeps throwing error. can't figure out why. please! list view has @android:id/list , have extended listactivity on activity here code fetch values
liststudent=getlistview(); studentmarkslist=new arraylist<hashmap<string,string>>(); string student,obtained_value,max_value; (int i=0;i<liststudent.getcount();i++) { view view=liststudent.getchildat(i); studentidtxt=(textview) view.findviewbyid(r.id.student_id_ls); obtainedtxt=(edittext) view.findviewbyid(r.id.obtained); maxtxt=(textview) view.findviewbyid(r.id.max); student=studentidtxt.gettext().tostring(); obtained_value=obtainedtxt.gettext().tostring(); max_value=maxtxt.gettext().tostring(); //updating new mark list array hashmap<string,string>studentmark=new hashmap<string,string>(); studentmark.put(tag_student_id,student); studentmark.put(tag_marks_obtained,obtained_value); studentmark.put(tag_marks_max,max_value); studentmarkslist.add(studentmark); }
and here xml layout list
<listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@android:id/list" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="106dp" />
and here xml list items
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/student_name_ls" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:paddingtop="6dip" android:textcolor="#1e5401" android:textsize="16sp" android:textstyle="bold" /> <!-- marks label --> <edittext android:id="@+id/obtained" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:textcolor="#acacac" android:inputtype="numberdecimal" android:numeric="decimal" /> <textview android:id="@+id/max" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:textcolor="#0b0b0b" android:enabled="false" android:editable="false" /> <textview android:id="@+id/student_id_ls" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:textcolor="#060606" android:visibility="gone" /> </linearlayout>
and problem on line "studentidtxt=(textview) view.findviewbyid(r.id.student_id_ls);" throws nullpointerexception please
you should use
liststudent.getchildcount()
this because number of views in list view equal number of views see. not equal number of items in listview. if use getcount() indexoutofboundsexception because there no more views.
Comments
Post a Comment