Android 在ExpandableListView中的onChildClickListener中获取组件
通过单击一个按钮,我会出现一个对话框,其布局中有一个ExpandableListView,并填充复选框:Android 在ExpandableListView中的onChildClickListener中获取组件,android,checkbox,expandablelistview,Android,Checkbox,Expandablelistview,通过单击一个按钮,我会出现一个对话框,其布局中有一个ExpandableListView,并填充复选框: button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog=new Dialog(MainActivity.this); dialog.setContentView
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;
}
}
公共类MyExpandableAdapter扩展了BaseExpandableListAdapter{
私人活动;
私人ArrayList儿童项目;
私人充气机;
私有ArrayList父项;
私人ArrayList儿童;
//建造师
公共MyExpandableAdapter(ArrayList父项、ArrayList子项)
{
this.parentItems=父项;
this.childtems=childern;
}
公共空隙设置充气机(充气机、活动)
{
这个。充气机=充气机;
这个。活动=活动;
}
@凌驾
public int getGroupCount(){
返回parentItems.size();
}
@凌驾
公共整数getChildrenCount(整数i){
return((ArrayList)childtems.get(i)).size();
}
@凌驾
公共对象getGroup(inti){
返回null;
}
@凌驾
公共对象getChild(inti,inti1){
返回null;
}
@凌驾
公共长getGroupId(inti){
返回0;
}
@凌驾
公共长getChildId(inti,inti1){
返回0;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int i、布尔b、视图转换视图、视图组视图组){
if(convertView==null){
convertView=充气机充气(R.layout.list\u组,空);
}
TextView TextView=(TextView)convertView.findViewById(R.id.TextView);
setText(parentItems.get(i));
//((TextView)convertView).setChecked(isExpanded);
返回视图;
}
@凌驾
公共视图getChildView(int i、int i1、布尔b、视图转换视图、视图组视图组){
child=(ArrayList)childtems.get(i);
if(convertView==null){
convertView=充气机。充气(R.layout.list_项,空);
}
复选框cb=(复选框)convertView.findViewById(R.id.CheckBox);
cb.setText(child.get(i1.getText());
cb.setFocusable(假);
cb.可设置可点击(错误);
返回视图;
}
@凌驾
公共布尔值是可选择的(int i,int i1){
返回true;
}
}
这样,在OnChildClickListener中,我通过访问相应的arraylist获得组件的名称(复选框文本中的内容)。但是复选框没有被选中…因此用户不知道复选框已经被选中
如何将复选框设置为单击状态,以便用户知道他/她的选择已完成?尝试以下操作:
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;
}