Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: CheckBox tree
    text
    copied!<p>In my application, I have a list and a button, in my list i want when i select parent item all of the child-item be selected indeed I want a Check-box tree. I use an Expandable List View but i have a problem in my code:</p> <p>My code is: list_group.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp" android:background="#000000"&gt; &lt;CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_marginLeft="30dp" android:layout_marginRight="2dp" android:focusable="false"/&gt; &lt;TextView android:id="@+id/lblListHeader" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" android:textSize="17dp" android:textColor="#ffffff" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>list_item2.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="55dip" android:orientation="horizontal" android:background="#ffffff"&gt; &lt;CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_marginLeft="30dp" android:layout_marginRight="2dp" android:focusable="false"/&gt; &lt;TextView android:id="@+id/lblListItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="17dip" android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_marginLeft="-40dp" android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>backup_list.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"&gt; &lt;ExpandableListView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/expandableListView"/&gt; &lt;Button android:id="@+id/backup" android:layout_gravity="center_horizontal" android:layout_width="150dp" android:layout_height="wrap_content" android:text="BackUp" android:clickable="true" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>How can i fix it?</p> <p>ExpandableListAdapter:</p> <pre><code>package com.mCloud.android.ui.activity; import java.util.HashMap; import java.util.List; import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.mCloud.android.R; public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List&lt;String&gt; _listDataHeader; // header titles // child data in format of header title, child title private HashMap&lt;String, List&lt;String&gt;&gt; _listDataChild; public ExpandableListAdapter(Context context, List&lt;String&gt; listDataHeader, HashMap&lt;String, List&lt;String&gt;&gt; listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item2, null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){ String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition){ return true; } } </code></pre> <p>ParentActivity:</p> <pre><code>package com.mCloud.android.ui.activity; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.widget.ExpandableListView; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuItem; import com.mCloud.android.Log_OC; import com.mCloud.android.R; import com.mCloud.android.filters.MediaFilenameFilter; public class ParentActivity extends SherlockFragmentActivity { private static final String TAG = "mCloudPreferences"; ExpandableListAdapter listAdapter; ExpandableListView expListView; List&lt;String&gt; listDataHeader; HashMap&lt;String, List&lt;String&gt;&gt; listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.backup_list); ActionBar actionBar = getSherlock().getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); // get the listview expListView = (ExpandableListView) findViewById(R.id.expandableListView); // preparing list data prepareListData(); listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); // setting list adapter expListView.setAdapter(listAdapter); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { super.onMenuItemSelected(featureId, item); Intent intent; switch (item.getItemId()) { case android.R.id.home: intent = new Intent(getBaseContext(), DisplayInfoActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; default: Log_OC.w(TAG, "Unknown menu item triggered"); return false; } return true; } /* * Preparing the list data */ private void prepareListData() { listDataHeader = new ArrayList&lt;String&gt;(); listDataHeader.add("Files"); listDataHeader.add("Apps"); listDataHeader.add("SMS"); listDataHeader.add("Contacts"); listDataHeader.add("Call Logs"); listDataHeader.add("Calender"); listDataHeader.add("Setting"); List&lt;String&gt; files = new ArrayList&lt;String&gt;(); File file = Environment.getExternalStorageDirectory(); String[] list = file.list(new MediaFilenameFilter()); if (list != null) { for (String name : list) files.add(name); } files.add("Advance Selection"); List&lt;String&gt; apps = new ArrayList&lt;String&gt;(); apps.add("All Apps"); List&lt;String&gt; sms = new ArrayList&lt;String&gt;(); sms.add("Inbox"); sms.add("Draft"); sms.add("Send"); List&lt;String&gt; contacts = new ArrayList&lt;String&gt;(); contacts.add("All Contacts"); List&lt;String&gt; calllogs = new ArrayList&lt;String&gt;(); calllogs.add("Resived"); calllogs.add("Missed"); calllogs.add("Dailed"); List&lt;String&gt; calender = new ArrayList&lt;String&gt;(); calender.add("All calender"); List&lt;String&gt; setting = new ArrayList&lt;String&gt;(); setting.add("All setting"); listDataChild = new HashMap&lt;String, List&lt;String&gt;&gt;(); listDataChild.put(listDataHeader.get(0), files); listDataChild.put(listDataHeader.get(1), apps); listDataChild.put(listDataHeader.get(2), sms); listDataChild.put(listDataHeader.get(3), contacts); listDataChild.put(listDataHeader.get(4), calllogs); listDataChild.put(listDataHeader.get(5), calender); listDataChild.put(listDataHeader.get(6), setting); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload