abhay_401 wrote:

> I am trying to create a custom expandable list where there is a group view 
> and child view will have a checkboxes.
>
> If i click on group view checkbox then all the child view checkboxes 
> should checked and vice versa...
> Basically I want to implement check all and uncheck all functionality.
> I have tried like this but which is not updating properly. Is anybody is 
> having any idea about this.
>

Please update us by describing the problem properly. What are the symptoms? 
 

>
> I am passing data something like this: 
> private static final String[][] data = 
> {{"audia4","audiq7","audir8"},{"bmwm6","bmwx6"},{"ferrarienzo","ferrarif430","ferrarif430italia"}};
>
> package com.test.sample;
>
> import java.util.HashMap;
> import android.app.Activity;
> import android.content.Context;
> import android.content.SharedPreferences;
> import android.preference.PreferenceManager;
> import android.text.Html;
> import android.util.Log;
> import android.view.LayoutInflater;
> import android.view.View;
> import android.view.ViewGroup;
> import android.widget.BaseExpandableListAdapter;
> import android.widget.CheckBox;
> import android.widget.CompoundButton;
> import android.widget.CompoundButton.OnCheckedChangeListener;
> import android.widget.ExpandableListAdapter;
>
> public class SampleExpandableListAdapter extends BaseExpandableListAdapter
> implements ExpandableListAdapter {
>  public Context context;
> CheckBox checkBox;
> private LayoutInflater vi;
>  private String[][] data;
> int _objInt;
>

Don't use underscores in mutable reference names.

You should organize your member variables in access-order order, absent a 
compelling 
reason to do otherwise. Also, static declarations should precede instance 
declarations.

Violating such things makes it harder for you and those who would help you 
to discern the 
problem.
 

> public static Boolean checked[] = new Boolean[1];
>

Why an array?
 

>
> HashMap<Long, Boolean> checkboxMap = new HashMap<Long, Boolean>();
> HashMap<Integer, Boolean> m_checkboxMap = new HashMap<Integer, Boolean>();
>

What's the difference between these two maps? You should declare them as 
type 'Map' rather than 
'HashMap'.
 

>  private static final int GROUP_ITEM_RESOURCE = R.layout.group_item;
> private static final int CHILD_ITEM_RESOURCE = R.layout.child_item;
>

Static constant variables to cover for static constant variables. You 
really don't need that.
 

>  public String[] check_string_array;
>

Use the Java naming conventions.
 

>
> // Abhi
> private int mgroupPos = 0;
>

"m" isn't really needed as a prefix.

Assigning '0' to a member 'int' explicitly causes it to be initialized to 
'0' twice. In this case 
I see that you wish to make it quite clear what the starting values must 
be. However, the 
comment is not helpful.
 

>  private static ViewHolder holder;
> static int m_groupPosition = 0;
>

Static variables should go first.
 

> public SampleExpandableListAdapter(Context context, Activity activity,
>  String[][] data) {
>

Javadoc comments?
 

> this.data = data;
> this.context = context;
>  vi = (LayoutInflater) activity
> .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
>

Why not 'this.vi'?
 

> _objInt = data.length;
>

What if 'data == null'?

Consistency aids maintainability, e.g., in the use of 'this.' within the 
constructor.
 

>  check_string_array = new String[_objInt];
>

Do you even need a variable to hold 'data.length', which is immutable?
 

> popolaCheckMap();
> }
>
> public void popolaCheckMap() {
>
> SharedPreferences settings = PreferenceManager
>  .getDefaultSharedPreferences(context);
> String buffer = null;
>

You don't use this initialization, so don't use this initialization.
 

>
> for (int i = 0; i < _objInt; i++) {
>  buffer = settings.getString(String.valueOf((int) i), "false");
>

You don't need to cast an 'int' to an 'int.

Why are you using 'getString()' and not 'getBoolean()'?

If you really, really, really, really must use 'String' values to represent 
'boolean' values, 
God knows why and I wish you'd tell us, you should set it with 
'Boolean.FALSE.toString()' 
and 'Boolean.TRUE.toString()', and retrieve the value with 
'Boolean.valueOf(String s)'.

Don't reinvent the wheel.
 

> if (buffer.equals("false"))
>  checkboxMap.put((long) i, false);
>

Why do you make the map '<Long, Boolean>' when your entries are 
'<Integer, Boolean>'?

Avoid autoboxing.

USE BRACES AROUND THE STATEMENT CONTROLLED BY THE CONTROL EXPRESSION!
 

> else
> checkboxMap.put((long) i, true);
>  }
> }
>
> public class CheckListener implements OnCheckedChangeListener {
>  long pos;
> int m_pos; 
>

> public void setPosition(long p) {
>  pos = p;
> }
>
> @Override
> public void onCheckedChanged(CompoundButton buttonView,
>  boolean isChecked) {
> Log.i("checkListenerChanged",
> String.valueOf(pos) + ":" + String.valueOf(isChecked));
>  checkboxMap.put(pos, isChecked);
> try {
> updateCheckbox();
>  } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
>  }
> if (isChecked == true)
> check_string_array[(int) pos] = "true";
>  else
> check_string_array[(int) pos] = "false";
> // save checkbox state of each group
>  SharedPreferences settings = PreferenceManager
> .getDefaultSharedPreferences(context);
>
 SharedPreferences.Editor preferencesEditor = settings.edit();
> preferencesEditor.putString(String.valueOf((int) pos),
>  check_string_array[(int) pos]);
>

You should stop using 'String' everywhere.
 

> preferencesEditor.commit();
> }
>  }
>
> public String getChild(int groupPosition, int childPosition) {
> return data[groupPosition][childPosition];
>  }
>
> public long getChildId(int groupPosition, int childPosition) {
> return childPosition;
>  }
>
> public int getChildrenCount(int groupPosition) {
> mgroupPos = groupPosition;
>  return data[groupPosition].length;
> }
>
> public View getChildView(int groupPosition, int childPosition,
>  boolean isLastChild, View convertView, ViewGroup parent) {
> m_groupPosition = groupPosition; 
> View v = convertView;
>  String child = getChild(groupPosition, childPosition);
> System.out.println("Abhi getChildView child: " + child);
>  int id_res = 0;
> if (groupPosition == 0) {
> if (childPosition == 0)
>  id_res = R.drawable.audi_a4;
> if (childPosition == 1)
> id_res = R.drawable.audi_q7;
>  if (childPosition == 2)
> id_res = R.drawable.audi_r8;
> } else if (groupPosition == 1) {
>  if (childPosition == 0)
> id_res = R.drawable.bmw_m6;
> if (childPosition == 1)
>  id_res = R.drawable.bmw_x6;
> } else if (groupPosition == 2) {
> if (childPosition == 0)
>  id_res = R.drawable.ferrari_enzo;
> if (childPosition == 1)
> id_res = R.drawable.ferrari_f430;
>  if (childPosition == 2)
> id_res = R.drawable.ferrari_f430_italia;
> }
>
> if (child != null) {
> v = vi.inflate(CHILD_ITEM_RESOURCE, null);
> holder = new ViewHolder(v);
>  holder.text.setText(Html.fromHtml(child));
>
> holder.imageview.setImageResource(id_res);
>  // Abhi
> CheckListener checkL = new CheckListener();
> // checkL.setPosition(group_id);
>  holder.checkbox.setOnCheckedChangeListener(checkL);
> // holder.checkbox.setChecked(checkboxMap.get(group_id));
>  // Abhi
>
> if(checkboxMap.get((long)groupPosition) == true){
>

You don't need to compare a 'boolean' to a 'boolean' to get a 'boolean'!
 

> holder.checkbox.setChecked(true);
>  }else{
>

You should use whitespace.
 

> holder.checkbox.setChecked(false);
> }
>  }
> return v;
> }
>
> public String getGroup(int groupPosition) {
>  return "group-" + groupPosition;
> }
>
> public int getGroupCount() {
>  return data.length;
>

See? Now here you use the right variable. Get rid of '_objInt'. (Ghastly 
name, BTW.)
 

> }
>
> public long getGroupId(int groupPosition) {
>  return groupPosition;
> }
>
> public View getGroupView(int groupPosition, boolean isExpanded,
>  View convertView, ViewGroup parent) {
> View v = convertView;
> String group = null;
>  int id_res = 0;
> long group_id = getGroupId(groupPosition);
> if (group_id == 0) {
>

Use a 'switch'.
 

>  group = "Audi";
> id_res = R.drawable.audi;
> } else if (group_id == 1) {
>  group = "BMW";
> id_res = R.drawable.bmw;
> } else if (group_id == 2) {
>  group = "Ferrari";
> id_res = R.drawable.ferrari;
> }
>
> if (group != null) {
>

This section would be evaded by an early out in the 'default' clause.
 

> v = vi.inflate(GROUP_ITEM_RESOURCE, null);
> ViewHolder holder = new ViewHolder(v);
>
> holder.text.setText(Html.fromHtml(group));
> holder.imageview.setImageResource(id_res);
>  holder.checkbox.setFocusable(false);
> CheckListener checkL = new CheckListener();
> checkL.setPosition(group_id);
>  holder.checkbox.setOnCheckedChangeListener(checkL);
> holder.checkbox.setChecked(checkboxMap.get(group_id));
>  notifyDataSetChanged();
> }
> return v;
> }
>
> public boolean isChildSelectable(int groupPosition, int childPosition) {
> return true;
>  }
>
> public boolean hasStableIds() {
> return true;
>  }
>  private void updateCheckbox(){
> System.out.println();
>  if(checkboxMap.get((long)m_groupPosition) == true){
>

if (true == true) 

What about 'holder.checkbox(setChecked(map.get(Integer.valueOf(position)))'?

Even better, what about using that specialized 'Map' from 'int' to '<E>' 
known as 'E[]' 
or 'List<E>' and saving yourself 75% of your code and 99% of your 
opportunity to have a bug?
 

> holder.checkbox.setChecked(true);
> }else{
>  holder.checkbox.setChecked(false);
> }
> }
> }
>


Bottom line: Your code is a right mess. Clean it up and the logic flaws, or 
lack thereof, 
will leap out at you.

-- 
Lew
 

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to