android - how to add function on child items in expandablelistview? -
good day! new in android programming , java, did copy paste of source code in internet worked have edit this.
i wanna ask how add function when click on switch of every group supposed on/off not toast. understand how use onclicklistener somehow dont know put function.
any answers or tutorial link appreciated thank you!
mainactivity.java
package com.capstone.r.e.d.e_kit; import java.util.arraylist; import android.app.expandablelistactivity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.widget.expandablelistview; public class mainactivity extends expandablelistactivity{ private arraylist<string> parentitems = new arraylist<string>(); private arraylist<object> childitems = new arraylist<object>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // not necessary expandablelistactivity contains expandablelist //setcontentview(r.layout.main); expandablelistview expandablelist = getexpandablelistview(); // can use (expandablelistview) findviewbyid(r.id.list) expandablelist.setdividerheight(2); expandablelist.setgroupindicator(null); expandablelist.setclickable(true); setgroupparents(); setchilddata(); mainactivityadapter adapter = new mainactivityadapter(parentitems, childitems); adapter.setinflater((layoutinflater) getsystemservice(context.layout_inflater_service), this); expandablelist.setadapter(adapter); expandablelist.setonchildclicklistener(this); } public void setgroupparents() { parentitems.add("flash light"); parentitems.add("whistle"); parentitems.add("infographics"); parentitems.add("battery saving mode"); } public void setchilddata() { // flash light arraylist<string> child = new arraylist<string>(); child.add("switch"); childitems.add(child); // whistle child = new arraylist<string>(); child.add("switch"); childitems.add(child); // information child = new arraylist<string>(); child.add("online news"); child.add("do's , dont's"); childitems.add(child); // battery saving mode child = new arraylist<string>(); child.add("switch"); childitems.add(child); } }
mainactivityadapter.java
package com.capstone.r.e.d.e_kit; import java.util.arraylist; import android.app.activity; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.checkedtextview; import android.widget.textview; import android.widget.toast; public class mainactivityadapter extends baseexpandablelistadapter { private activity activity; private arraylist<object> childtems; private layoutinflater inflater; private arraylist<string> parentitems, child; public mainactivityadapter(arraylist<string> parents, arraylist<object> childern) { this.parentitems = parents; this.childtems = childern; } public void setinflater(layoutinflater inflater, activity activity) { this.inflater = inflater; this.activity = activity; } @suppresswarnings("unchecked") @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { child = (arraylist<string>) childtems.get(groupposition); textview textview = null; if (convertview == null) { convertview = inflater.inflate(r.layout.group, null); } textview = (textview) convertview.findviewbyid(r.id.textview1); textview.settext(child.get(childposition)); convertview.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { toast.maketext(activity, child.get(childposition), toast.length_short).show(); } }); return convertview; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { if (convertview == null) { convertview = inflater.inflate(r.layout.row, null); } ((checkedtextview) convertview).settext(parentitems.get(groupposition)); ((checkedtextview) convertview).setchecked(isexpanded); return convertview; } @override public object getchild(int groupposition, int childposition) { return null; } @override public long getchildid(int groupposition, int childposition) { return 0; } @override public int getchildrencount(int groupposition) { return ((arraylist<string>) childtems.get(groupposition)).size(); } @override public object getgroup(int groupposition) { return null; } @override public int getgroupcount() { return parentitems.size(); } @override public void ongroupcollapsed(int groupposition) { super.ongroupcollapsed(groupposition); } @override public void ongroupexpanded(int groupposition) { super.ongroupexpanded(groupposition); } @override public long getgroupid(int groupposition) { return 0; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int groupposition, int childposition) { return false; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <expandablelistview android:id="@+id/list" android:layout_height="match_parent" android:layout_width="match_parent" android:groupindicator="@null" /> </linearlayout>
row.xml
<checkedtextview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_marginleft="5dp" android:gravity="center_vertical" android:padding="10dp" android:textcolor="#ffffff" android:textsize="15sp" android:background="#339966" />
group.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="40dp" android:background="@android:color/white" android:clickable="true" android:orientation="vertical" android:paddingleft="40dp" tools:context=".mainactivity" > <linearlayout android:layout_width="match_parent" android:layout_height="39dp" android:gravity="center_vertical"> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:textisselectable="true" android:textcolor="#000000" android:textsize="14sp" /> </linearlayout> </linearlayout>
try set setongroupclicklistener
:
adapter.setinflater((layoutinflater) getsystemservice(context.layout_inflater_service), this); expandablelist.setadapter(adapter); expandablelist.setonchildclicklistener(this); expandablelist.setongroupclicklistener(this); //implement inteface expandablelistview.ongroupclicklistener
tutorial: http://www.vogella.com/tutorials/androidlistview/article.html#expandablelistview
Comments
Post a Comment