java - Android ExpandableList onChildClickListener in Fragment does not work -
i have code onchildclicklistener snippet code below
onchildclicklistener occl = new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { // todo auto-generated method stub toast.maketext(getactivity(), "jalan", toast.length_long).show(); return true; } }; lv2.setonchildclicklistener(occl);
but toast does'nt appear, why can occurring?
depending 2 files, (sorry long post, want clear)
the below snipped adapter code:
package com.em.crayonpediamoodlesiswa; import java.util.arraylist; import java.util.hashmap; import java.util.list; import android.content.context; import android.content.entity; import android.graphics.typeface; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.expandablelistview; import android.widget.textview; public class expandablelistadapter extends baseexpandablelistadapter { private context context; private list<string> expandablelisttitle; private hashmap<string, list<string>> expandablelistdetail; arraylist<entity> objects; view rootview; expandablelistadapterchild la; expandablelistview lv; list<string> listdataheader; hashmap<string, list<string>> listdatachild; public expandablelistadapter(context context, list<string> expandablelisttitle, hashmap<string, list<string>> expandablelistdetail) { this.context = context; this.expandablelisttitle = expandablelisttitle; this.expandablelistdetail = expandablelistdetail; } @override public object getchild(int listposition, int expandedlistposition) { return this.expandablelistdetail.get(this.expandablelisttitle.get(listposition)) .get(expandedlistposition); } @override public long getchildid(int listposition, int expandedlistposition) { return expandedlistposition; } @override public view getchildview(int listposition, final int expandedlistposition, boolean islastchild, view convertview, viewgroup parent) { final string expandedlisttext = (string) getchild(listposition, expandedlistposition); if (convertview == null) { layoutinflater layoutinflater = (layoutinflater) this.context .getsystemservice(context.layout_inflater_service); convertview = layoutinflater.inflate(r.layout.list_item, null); } textview expandedlisttextview = (textview) convertview .findviewbyid(r.id.expandedlistitem); expandedlisttextview.settext(expandedlisttext); /*expandablelistview elv = new expandablelistview(context); elv.setlayoutparams(new abslistview.layoutparams(abslistview.layoutparams.wrap_content, abslistview.layoutparams.wrap_content)); elv.setadapter(new expandablelistadapter(context,null,null)); ((viewgroup)convertview).addview(elv);*/ return convertview; } @override public int getchildrencount(int listposition) { return this.expandablelistdetail.get(this.expandablelisttitle.get(listposition)) .size(); } @override public object getgroup(int listposition) { return this.expandablelisttitle.get(listposition); } @override public int getgroupcount() { return this.expandablelisttitle.size(); } @override public long getgroupid(int listposition) { return listposition; } @override public view getgroupview(int listposition, boolean isexpanded, view convertview, viewgroup parent) { string listtitle = (string) getgroup(listposition); if (convertview == null) { layoutinflater layoutinflater = (layoutinflater) this.context. getsystemservice(context.layout_inflater_service); convertview = layoutinflater.inflate(r.layout.list_group, null); } textview listtitletextview = (textview) convertview .findviewbyid(r.id.listtitle); listtitletextview.settypeface(null, typeface.bold); listtitletextview.settext(listtitle); return convertview; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int listposition, int expandedlistposition) { return true; } }
the below snippet activity code using fragment:
package com.em.crayonpediamoodlesiswa; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; import java.util.arraylist; import java.util.hashmap; import java.util.list; import android.os.bundle; import android.app.activity; import android.content.intent; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.expandablelistview; import android.widget.expandablelistview.onchildclicklistener; import android.widget.toast; public class mycourses extends fragment{ private static final string url = "jdbc:mysql://192.168.43.166:3306/moodle"; private static final string user = "emil"; private static final string pass = "emilhamep"; private int amount; expandablelistadapter la; expandablelistview lv2; list<string> expandablelisttitle; hashmap<string, list<string>> expandablelistdetail; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.my_courses, container, false); lv2=(expandablelistview)rootview.findviewbyid(r.id.expandablelistviewchild); preparelistdatamycourse(); la=new expandablelistadapter(getactivity(), expandablelisttitle, expandablelistdetail); lv2.setadapter(la); //menentukan aksi ketika diklik untuk setiap hasil fetch array try{ //toast.maketext(getactivity(), "jalan-jalan", toast.length_long).show(); int user_id = 5; class.forname("com.mysql.jdbc.driver"); connection con=drivermanager.getconnection(url,user,pass); statement st = con.createstatement(); resultset rschild=st.executequery("select co.fullname course co join course_display codi codi.userid=" + user_id + " , co.id= codi.course"); final string[] = null; int counter=0; while(rschild.next()) { a[counter]=rschild.getstring(1); //my_course.add(rschild.getstring(1)); counter++; } /* lv2.setonchildclicklistener(new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { toast.maketext(getactivity(), "jalan", toast.length_long).show(); for(int b=0;b<a.length;b++){ if(childposition == b){ toast.maketext(getactivity(), a[b], toast.length_long).show(); } intent intent = new intent(getactivity(),classroom.class); string logged_user=a[b].tostring(); intent.putextra("username", logged_user); startactivity(intent); } return false; } });*/ onchildclicklistener occl = new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { // todo auto-generated method stub toast.maketext(getactivity(), "jalan", toast.length_long).show(); return true; } }; lv2.setonchildclicklistener(occl); } catch(exception e){ e.printstacktrace(); toast.maketext(getactivity(), e.tostring(), toast.length_long); } return rootview; } private void preparelistdatamycourse(){ expandablelisttitle = new arraylist<string>(); expandablelistdetail = new hashmap<string, list<string>>(); try{ int user_id = 5; class.forname("com.mysql.jdbc.driver"); connection con=drivermanager.getconnection(url,user,pass); statement st = con.createstatement(); //query header expandable list<string> my_course = new arraylist<string>(); expandablelisttitle.add("my course"); //query dapet list course resultset rschild=st.executequery("select co.fullname course co join course_display codi codi.userid=" + user_id + " , co.id= codi.course"); while(rschild.next()) { my_course.add(rschild.getstring(1)); } expandablelistdetail.put(expandablelisttitle.get(0), my_course); }catch(exception e){ e.printstacktrace(); toast.maketext(getactivity(), e.tostring(), toast.length_long); } } }
check adapter ischildselectable returns true or false.return true @override public boolean ischildselectable(int groupposition, int childposition) { return true; }
Comments
Post a Comment