在expandableListView的childView中使用的CheckBox,已经实现了界面的显示,
但是想实现,当点了checkBox得某一项以后,获取该CheckBox的值,并在旁边的TextView中显示出来,求高手解答!

解决方案 »

  1.   

    package com.demo.android.idr;import java.util.ArrayList;
    import java.util.List;import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.ExpandableListView;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.ExpandableListView.OnGroupClickListener;public class ProtocolActivity extends Activity { private Button cancelButton;
    private Button acceptButton;
    private TextView protocolTextVIew;
    private ExpandableListView expandableListView;
    private CustomExpandAdapter customExpandAdapter;
    private List<String> group;
    private List<List<String>> child; @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.protocol);
    ManageApplication.getInstance().addActivity(this);
    addExamle();
    findViews();
    setListeners();

             // to refresh ExpandableListView
    customExpandAdapter.notifyDataSetChanged();
    } private void findViews() {
    // ExpandableListView & BaseExpandableListAdapter
    expandableListView = (ExpandableListView) findViewById(R.id.expandableList);
    customExpandAdapter = new CustomExpandAdapter(this);
    expandableListView.setAdapter(customExpandAdapter);
    cancelButton = (Button) findViewById(R.id.cancelButton);
    acceptButton = (Button) findViewById(R.id.acceptButton);
    protocolTextVIew = (TextView) findViewById(R.id.protocolsTextView);

        
    } public void addExamle() {
    // to initialize 2 contains
    group = new ArrayList<String>();
    child = new ArrayList<List<String>>(); addItem("Abdomen", new String[] { "ABD_1_VIEW", "ABD_2_VIEW" });
    addItem("Chest", new String[] { "CHEST_1_VIEW", "CHEST_2_VIEW",
    "RIBS_ABOVE_DIAPHRAGM", "RIBS_BELOW_DIAPHRAGM", "STERNUM" });
    addItem("Shoulder", new String[] { "SHOULDER", "SCAPULA", "AC_JOINTS",
    "CS_JOINTS", "CLAVICLE" });
    } public void addItem(String parentString, String[] childString) {
    group.add(parentString); List<String> item = new ArrayList<String>(); for (int i = 0; i < childString.length; i++) {
    item.add(childString[i]);
    } child.add(item);
    } public class CustomExpandAdapter extends BaseExpandableListAdapter {
    Activity activity; public CustomExpandAdapter(Activity a) {
    activity = a;
    } // child method stub // @Override
    public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    //String string=child.get(groupPosition).get(childPosition).toString();

    return child.get(groupPosition).get(childPosition);
    } // @Override
    public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
    } // @Override
    public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return child.get(groupPosition).size();
    } // @Override
    public View getChildView(int groupPosition, int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    String string = child.get(groupPosition).get(childPosition);
    // return getGenericView(string);
    return composeCheckView(string);
    } // group method stub
    // @Override
    public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return group.get(groupPosition);
    } // @Override
    public int getGroupCount() {
    // TODO Auto-generated method stub
    return group.size();
    } // @Override
    public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
    } // @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    String string = group.get(groupPosition);
    return getGenericView(string);
    } // View stub to create Group/Children 's View
    public TextView getGenericView(String s) {
    // Layout parameters for the ExpandableListView
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity);
    text.setLayoutParams(lp);
    // Center the text vertically
    text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    // Set the text starting position
    text.setPadding(36, 0, 0, 0); text.setText(s);
    return text;
    } // @Override
    public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
    } // @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
    } // custom method stub
    private View composeCheckView(String string) {
    CheckBox cb = new CheckBox(activity);
    cb.setText(string);
    return cb;
    } } private void setListeners() {
    acceptButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent protocolExpoParameterIntent = new Intent();
    protocolExpoParameterIntent.setClass(ProtocolActivity.this,
    ExpoParameterActivity.class);
    startActivity(protocolExpoParameterIntent);
    ProtocolActivity.this.finish();
    }
    }); cancelButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent protocolWorklistIntent = new Intent();
    protocolWorklistIntent.setClass(ProtocolActivity.this,
    WorklistActivity.class);
    startActivity(protocolWorklistIntent);
    ProtocolActivity.this.finish();
    }
    });

    expandableListView.setOnGroupClickListener(new OnGroupClickListener() {  
               // @Override  
                public boolean onGroupClick(ExpandableListView arg0, View arg1,  
                        int arg2, long arg3) {  
                    // TODO Auto-generated method stub  
                    System.out.println("The row id of the group clicked" + arg3);  
                    Toast.makeText(ProtocolActivity.this, "[Group Click]:" + arg2,  
                            Toast.LENGTH_SHORT).show();  
                    return false;  
                }  
            });  
    expandableListView.setOnChildClickListener(new OnChildClickListener() {  
               // @Override  
                public boolean onChildClick(ExpandableListView arg0, View arg1,  
                        int arg2, int arg3, long arg4) {  
                    // TODO Auto-generated method stub  
                    Toast.makeText(ProtocolActivity.this, "[Child Click]:" + arg2 + ":" + arg3,  
                            Toast.LENGTH_SHORT).show();  
                    return false;  
                }  
            });  

    }
    }
      

  2.   

    <?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/dark_blue"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" 
    android:paddingBottom="10dp">
    <LinearLayout android:id="@+id/leftLayout"
    android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="378dp">
    <TextView android:id="@+id/availableProtocolsTextView"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:text="@string/availableProtocols" android:paddingLeft="10dp"
    android:textColor="@color/dark_blue_text" android:background="@color/mid_blue"
    android:textSize="25dp" android:textStyle="bold"></TextView>
    <ExpandableListView android:id="@+id/expandableList"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:background="@color/dark_grey" 
    android:textSize="25dp"></ExpandableListView>
    </LinearLayout>  <LinearLayout 
    android:id="@+id/rightLayout"
    android:orientation="vertical"
    android:layout_width="wrap_content" android:layout_height="fill_parent"
    android:layout_weight="3.18" android:weightSum="1"
    android:layout_marginLeft="10dp" 
    android:background="@color/dark_blue">
    <TextView android:id="@+id/selectedProtocolsTextView"
    android:layout_width="fill_parent"
    android:text="@string/selectedProtocols" android:paddingLeft="10dp"
    android:textColor="@color/dark_blue_text" android:background="@color/mid_blue"
    android:textSize="25dp" android:textStyle="bold" android:layout_height="wrap_content"></TextView>
    <TextView android:id="@+id/protocolsTextView"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:text="@string/Protocols" android:textColor="@color/black"
    android:background="@color/light_grey" android:paddingLeft="10dp"
    android:textSize="25dp" android:layout_weight="0.91"></TextView>
    <LinearLayout android:orientation="horizontal"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.08"
    android:background="@color/light_blue" 
    android:gravity="center_vertical"
    android:paddingLeft="450dp"
    android:paddingRight="20dp"
    android:layout_marginTop="10dp">
    <Button android:id="@+id/acceptButton" 
      android:layout_width="wrap_content"
          android:text="@string/acceptButtonTitle"
          android:textColor="@color/black" 
          android:background="@color/blue_button"
      android:textSize="20dp" 
      android:textStyle="bold"
          android:layout_weight="1" 
          android:layout_height="50dp"/>
          <Button android:id="@+id/cancelButton" 
      android:layout_width="wrap_content"
          android:text="@string/cancelButtonTitle"
          android:background="@color/blue_button"
          android:textColor="@color/black" 
      android:textSize="20dp" 
      android:textStyle="bold"
          android:layout_weight="1" 
          android:layout_height="50dp"
          android:layout_marginLeft="30dp"/>
    </LinearLayout>


    </LinearLayout></LinearLayout>