1

我在android中有一个程序,我在expandableListView组上有一个按钮,我有4组,所以我需要单独控制这4个按钮,每个按钮都有不同的活动。谁能帮我?

Code for MainActivity:
public class MainActivity extends Activity {
final Context context = this;
private static final String[][] data = {{" "},{"a1","a2"},{"s1","s2","s3"},{"t1","t2","t3"}};

private ExpandableListView expandableListView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView)findViewById(R.id.lvExp);
expandableListView.setAdapter(new ExpandableListAdapter(context, this, data));
}




Here`s my code for Adapter:
class ExpandableListAdapter extends BaseExpandableListAdapter {
        public Context context;
        CheckBox checkBox;
    private LayoutInflater vi;
    private String[][] data;
    int _objInt;
    public static Boolean checked[] = new Boolean[1];

    HashMap<Long,Boolean> checkboxMap = new HashMap<Long,Boolean>();
    private static final int GROUP_ITEM_RESOURCE = R.layout.list_group;
    private static final int CHILD_ITEM_RESOURCE = R.layout.list_item;
    public String []check_string_array;

    public ExpandableListAdapter(Context context, Activity activity, String[][] data) {
        this.data = data;
        this.context = context;
        vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        _objInt = data.length;
        check_string_array = new String[_objInt];
        popolaCheckMap();
    }
    public void popolaCheckMap(){

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
        String buffer = null;

        for(int i=0; i<_objInt; i++){
                buffer = settings.getString(String.valueOf((int)i),"false");
                if(buffer.equals("false"))
                        checkboxMap.put((long)i, false);
                else checkboxMap.put((long)i, true);
        }
    }

    public class CheckListener implements OnCheckedChangeListener{

        long 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);
            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]);
            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) {
        return data[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = convertView;
        String child = getChild(groupPosition, childPosition);
        int id_res = 0;


        if(groupPosition == 0){
                if(childPosition == 0) id_res = R.drawable.ic_launcher;

        }
        else if(groupPosition == 1){

        }
        else if(groupPosition == 2){

        }
        else if(groupPosition == 3){
  }

        if (child != null) {
            v = vi.inflate(CHILD_ITEM_RESOURCE, null);
            ViewHolder holder = new ViewHolder(v);
            holder.text.setText(Html.fromHtml(child));

            holder.imageview.setImageResource(id_res);
        }
        return v;
    }
    public String getGroup(int groupPosition) {
        return "group-" + groupPosition;
    }
    public int getGroupCount() {
        return data.length;
    }
    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){
                group = "Power Consumption";

        }
        else if(group_id == 1){
                group = "Appliances";

        }
        else if(group_id == 2){
                group = "Scenes";

        }
        else if(group_id == 3){
            group = "Triggers";

    }



        if (group != null) {
            v = vi.inflate(GROUP_ITEM_RESOURCE, null);
            ViewHolder holder = new ViewHolder(v);

            holder.text.setText(Html.fromHtml(group));
            holder.imageview.setImageResource(id_res);
            holder.imageview.setFocusable(false);          
            CheckListener checkL = new CheckListener();
            checkL.setPosition(group_id);

        }
        return v;
    }
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    public boolean hasStableIds() {
        return true;
    }
}



class ViewHolder {
    public TextView text;
    public ImageView imageview;
    public ViewHolder(View v) {
        this.text = (TextView)v.findViewById(R.id.text1);
        this.imageview = (ImageView)v.findViewById(R.id.image1);
    }

}
4

0 回答 0