2016-10-04 43 views
1

通過點擊一個按鈕,我得到出現一個對話框,在其佈局的ExpandableListView,被填充複選框:獲取部件內部onChildClickListener在ExpandableListView

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      dialog=new Dialog(MainActivity.this); 
      dialog.setContentView(R.layout.selectdialoglayout); 
      elv=(ExpandableListView) dialog.findViewById(R.id.elv); 
      setParentsItems(); 
      setChildData(); 
      MyExpandableAdapter adapter = new MyExpandableAdapter(parents, childs); 
      adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), MainActivity.this); 
      elv.setAdapter(adapter); 
      elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
       @Override 
       public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { 

        String componente=""; 
        if(i==0){ 
         componente=childsIuGeneral.get(i1); 
        } 
        if(i==1){ 
         componente=childsIuSuelo.get(i1); 
        } 
        if(i==2){ 
         componente=childsIuCon.get(i1); 
        } 
        if(i==3){ 
         componente=childsIuComp.get(i1); 
        } 
        if(i==4){ 
         componente=childsIuAmort.get(i1); 
        } 
        Log.i("LOG", "Componente seleccionado: "+componente); 
        return true; 
       } 
      }); 
      Button close=(Button)dialog.findViewById(R.id.button2); 
      close.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        dialog.dismiss(); 
       } 
      }); 
      dialog.show(); 
     } 
    }); 

適配器:

public class MyExpandableAdapter extends BaseExpandableListAdapter { 

private Activity activity; 
private ArrayList<Object> childtems; 
private LayoutInflater inflater; 
private ArrayList<String> parentItems; 
private ArrayList<CheckBox> child; 

// constructor 
public MyExpandableAdapter(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; 
} 


@Override 
public int getGroupCount() { 
    return parentItems.size(); 
} 

@Override 
public int getChildrenCount(int i) { 
    return ((ArrayList<CheckBox>) childtems.get(i)).size(); 
} 

@Override 
public Object getGroup(int i) { 
    return null; 
} 

@Override 
public Object getChild(int i, int i1) { 
    return null; 
} 

@Override 
public long getGroupId(int i) { 
    return 0; 
} 

@Override 
public long getChildId(int i, int i1) { 
    return 0; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) { 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.list_group, null); 
    } 
    TextView textView=(TextView)convertView.findViewById(R.id.textView); 
    textView.setText(parentItems.get(i)); 
    //((TextView) convertView).setChecked(isExpanded); 
    return convertView; 
} 

@Override 
public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) { 
    child = (ArrayList<CheckBox>) childtems.get(i); 

    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.list_item, null); 
    } 
    CheckBox cb=(CheckBox)convertView.findViewById(R.id.checkBox); 
    cb.setText(child.get(i1).getText()); 
    cb.setFocusable(false); 
    cb.setClickable(false); 

    return convertView; 
} 

@Override 
public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 
} 

這樣,在OnChildClickListener中,我通過訪問相應的數組列表來獲取組件的名稱(CheckBox文本中的內容)。但CheckBox沒有被選中...所以用戶不知道該複選框已被選中。

如何將複選框設置爲單擊,以便用戶可以知道他/她的選擇已完成?

回答

1

試試這個:

public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { 
    /* your code */ 
    CheckBox cb=(CheckBox)view.findViewById(R.id.checkBox); 
    //if you want just set to true 
    cb.setChecked(!cb.isChecked()); 
    return true; 
}