java - Failed using ListAdapter in extends Fragment -
i trying update json data listview. failed when using listadapter. fragment didnt allow ? insist want use extends fragment. there method on ?
please me. thanks.
here part of whole code.
public class schedulefragment extends fragment { public progressdialog pdialog; private listview mylistview; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> subjectlist; // url subjects list private static string url_all_subjects = "http://192.168.1.12/android_project/get_subjects.php"; // json node names private static final string tag_success = "success"; private static final string tag_student = "students"; private static final string tag_matrix_id = "matrix"; private static final string tag_name = "name"; // subject jsonarray jsonarray subject = null; public schedulefragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_schedule, container, false); //------------------------------------creating listview----------------------- // loading subject in background thread new loadallsubject().execute(); // hashmap listview subjectlist = new arraylist<hashmap<string, string>>(); mylistview = (listview) rootview.findviewbyid(r.id.textviewmatrix); // on selecting single subject mylistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string matrix_id = ((textview) view.findviewbyid(r.id.textviewmatrix)).gettext() .tostring(); // starting new intent intent in = new intent(getactivity().getapplicationcontext(), singlesubject.class); // sending matrix id next activity in.putextra(tag_matrix_id, matrix_id); // starting new activity , expecting response startactivityforresult(in, 100); } }); return rootview; } /** * background async task load product making http request * */ class loadallsubject extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = progressdialog.show(schedulefragment.this.getactivity(), "progress", "loading subjects. please wait...", false); //pdialog.setmessage("loading subjects. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); } /** * getting products url * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_subjects, "get", params); // check log cat json reponse log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of subject subject = json.getjsonarray(tag_student); // looping through subjects (int = 0; < subject.length(); i++) { jsonobject c = subject.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_matrix_id); string name = c.getstring(tag_name); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_matrix_id, id); map.put(tag_name, name); // adding hashlist arraylist subjectlist.add(map); } } else { // no products found // launch add new product activity } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( getactivity(), subjectlist, r.layout.all_subject, new string[] { tag_matrix_id, tag_name}, new int[] { r.id.matrix_id, r.id.name }); // updating listview mylistview.setlistadapter(adapter); } } } }
this all_subject.xml
<!-- subject matrix_id - hidden - used pass other activity --> <textview android:id="@+id/matrix_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <!-- name label --> <textview android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingtop="6dip" android:paddingleft="6dip" android:textsize="17dip" android:textstyle="bold" />
you need extend listframgent
. setlistadapter
method of listfragment
.
no need use runonuithread
. onpostexecute
invoked on ui thread.
edit:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_schedule, container, false); subjectlist = new arraylist<hashmap<string, string>>(); mylistview = (listview) rootview.findviewbyid(r.id.textviewmatrix); new loadallsubject().execute(); mylistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string matrix_id = ((textview) view.findviewbyid(r.id.textviewmatrix)).gettext() .tostring(); // starting new intent intent in = new intent(getactivity().getapplicationcontext(), singlesubject.class); // sending matrix id next activity in.putextra(tag_matrix_id, matrix_id); // starting new activity , expecting response startactivityforresult(in, 100); } }); return rootview; }
then
protected void onpostexecute(string file_url) { super.onpostexecute(file_url); pdialog.dismiss(); listadapter adapter = new simpleadapter( getactivity(), subjectlist, r.layout.all_subject, new string[] { tag_matrix_id, tag_name}, new int[] { r.id.matrix_id, r.id.name }); mylistview.setadapter(adapter); }
Comments
Post a Comment