如何获取扩展列表视图项目中的复选框的ID? [英] how to get id of check boxes in expanded list view item?

查看:200
本文介绍了如何获取扩展列表视图项目中的复选框的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展列表视图,在父节点以及子项目中有复选框。
所有数据来自web服务,因此其动态。



附加的图片:





现在在菜单项上点击我想获取所有复选框状态。



附加的代码:

  / ** 
*
* /


public class Object_SecurityActivity extends ExpandableListActivity {
@ Override
public boolean onChildClick(ExpandableListView parent,View v,
int groupPosition,int childPosition,long id){
// TODO自动生成方法存根
return super.onChildClick v,groupPosition,childPosition,id);
}

@Override
public void onContentChanged(){
// TODO自动生成方法存根
super.onContentChanged();
}

private AndroidClientEntity obj_android_client;
private static final String LOG_TAG =ElistCBox2;
private String username,password,clientname;

@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
//setContentView (R.layout.main);
Intent security_intent = getIntent();
String id = security_intent.getStringExtra(id);
obj_android_client =(AndroidClientEntity)getApplicationContext();
username = obj_android_client.getUsername();
password = obj_android_client.getPassword();
clientname = obj_android_client.getClientName();
new Securityasync()。execute(username,password,clientname,id);

}

类Securityasync扩展AsyncTask< String,String,String> {
String sesurity_response = null;
ProgressDialog对话框;
private Expandable_list_Adapter expListAdapter;

@Override
protected String doInBackground(String ... params){
if((isOnline(Object_SecurityActivity.this).equals(true))){
Security_service security_obj = new Security_service();
try {
sesurity_response = security_obj.getUsersRoles(params [0],
params [1],params [2],params [3]);
} catch(IllegalStateException e){
e.printStackTrace();
} catch(JSONException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
return null;
}

@Override
protected void onPostExecute(String result){
if(isOnline(Object_SecurityActivity.this).equals(true)){

setContentView(R.layout.layout_expandable_listview);
ArrayList< String> groupNames = new ArrayList< String>();
ArrayList< String> sub = new ArrayList< String>();
ArrayList< ArrayList< String>> child = new ArrayList< ArrayList< String>>();
ArrayList< String> sub_id = new ArrayList< String>();
ArrayList< String> objrid = new ArrayList< String>();
try {
JSONArray json = new JSONArray(ssurity_response);
// JSONArray json_child = new JSONArray(sesurity_response);
for(int i = 0; i JSONObject json_obj = json.getJSONObject(i);
if(json_obj.getString(PRid)。equalsIgnoreCase(0)){

String ObjectRid = json_obj.getString(ObjectRid);
int m = 0;
objrid.add(m,ObjectRid);
m ++;
groupNames.add(json_obj.getString(Name));
for(int j = 0; j< json.length(); j ++){

JSONObject json_child = json.getJSONObject(j);
if(ObjectRid.equalsIgnoreCase(json_child
.getString(PRid))){
int n = 0;
sub_id.add(n,json_child.getString(ObjectRid));
sub.add(json_child.getString(Name));
}

}
child.add(sub);

}

}
expListAdapter = new Expandable_list_Adapter(getBaseContext(),
groupNames,child);
setListAdapter(expListAdapter);
Log.e(size in error,son+ json.length());
} catch(JSONException e){
Log.e(,,e);
Toast.makeText(getBaseContext(),parsing error,
Toast.LENGTH_LONG).show();
}
Log.e(sizeof list,+ sub_id.size());
迭代器itr = objrid.iterator();


while(itr.hasNext()){
Log.e(id,value+ itr.next());
}

}
}

@Override
protected void onPreExecute(){
super.onPreExecute();

}
}

}

和适配器类:

  public class Expandable_list_Adapter extends BaseExpandableListAdapter implements OnCheckedChangeListener {

private Context context;
private ArrayList< String> groupNames;
private ArrayList< ArrayList< String>>儿童;
private LayoutInflater充气器;

public Expandable_list_Adapter(Context context,
ArrayList< String> groupNames,
ArrayList< ArrayList< String>> child){
this.context = context;
this.groupNames = groupNames;
this.child = child;
inflater = LayoutInflater.from(context);
}

public Object getChild(int groupPosition,int childPosition){
return child.get(groupPosition).get(childPosition);
}

public long getChildId(int groupPosition,int childPosition){
return(long)(groupPosition * 1024 + childPosition); //每组最大1024个孩子
}

public查看getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent){
View v = null;
if(convertView!= null)
v = convertView;
else
v = inflater.inflate(R.layout.child_row,parent,false);
String c =(String)getChild(groupPosition,childPosition);
TextView color =(TextView)v.findViewById(R.id.childname);
if(color!= null)
color.setText(c);

CheckBox cb =(CheckBox)v.findViewById(R.id.check1);

//cb.setChecked(false);
cb.setOnCheckedChangeListener(this);
return v;
}

public int getChildrenCount(int groupPosition){
return child.get(groupPosition).size();
}

public Object getGroup(int groupPosition){
return groupNames.get(groupPosition);
}

public int getGroupCount(){
return groupNames.size();
}
public long getGroupId(int groupPosition){
return(long)(groupPosition * 1024); //与getChildId一致
}

public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent){
View v = null;
if(convertView!= null)
v = convertView;
else
v = inflater.inflate(R.layout.group_row,parent,false);
String gt =(String)getGroup(groupPosition);
TextView colorGroup =(TextView)v.findViewById(R.id.childname);
if(gt!= null)
colorGroup.setText(gt);
CheckBox cb =(CheckBox)v.findViewById(R.id.check2);
cb.setChecked(false);
return v;
}

public boolean hasStableIds(){
return true;
}

public boolean isChildSelectable(int groupPosition,int childPosition){
Log.e(is group checked,group+ groupPosition);
Log.e(selectable,has+ childPosition);
return true;
}

public void onGroupCollapsed(int groupPosition){}
public void onGroupExpanded(int groupPosition){}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
// TODO自动生成方法存根

}
public void isChecked(){

}

}

Child.xml

 <?xml version =1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =horizo​​ntal
android:background =#21415A
android:layout_width =fill_parent
android:layout_height =wrap_content>

&TextView android:id =@ + id / childname
android:paddingLeft =50px
android:focusable =false
android: textSize =14px
android:textStyle =italic
android:layout_width =wrap_content
android:layout_height =wrap_content/>

&TextView android:id =@ + id / rgb
android:focusable =false
android:textSize =14px
android: textStyle =italic
android:layout_width =100px
android:layout_height =wrap_content/>

< CheckBox
android:id =@ + id / check1
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentRight =true
android:layout_alignParentTop =true
android:focusable =false/>

< / RelativeLayout>

parent.xml与child相同。



请指导我如何获得复选框的ID,因为在菜单项选择我必须做操作基础上。



编辑:我试图setTag和getTag()。但现在可扩展列表视图显示奇怪的行为。当我选择一个复选框并展开另一个组所有chekbok框设置为默认。
在这种情况下我要做的。我不知道resion为什么它不保存状态。
i我面临着与 Expandablelistview中的奇怪行为 - Android 相同的问题
Plz指南

解决方案

终于解决了我的问题。
适配器类:

  public class Expandable_list_Adapter extends BaseExpandableListAdapter implements OnCheckedChangeListener {

private Context context ;
private ArrayList< String> groupNames;
private ArrayList< ArrayList< String>>儿童;
private LayoutInflater充气器;

public Expandable_list_Adapter(Context context,
ArrayList< String> groupNames,
ArrayList< ArrayList< String>> child){
AndroidClientEntity.objHasmap.clear ;
this.context = context;
this.groupNames = groupNames;
this.child = child;
inflater = LayoutInflater.from(context);
}

public Object getChild(int groupPosition,int childPosition){
return child.get(groupPosition).get(childPosition);
}

public long getChildId(int groupPosition,int childPosition){
return(long)(groupPosition * 1024 + childPosition); //每组最大1024个孩子
}

public查看getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent){
View v = null;
if(convertView!= null)
v = convertView;
else
v = inflater.inflate(R.layout.child_row,parent,false);
String c =(String)getChild(groupPosition,childPosition);
Log.e(hiiiiiiiiiiiiiiiiiiiiiiiiiiii,c);

TextView label =(TextView)v.findViewById(R.id.childname);
if(label!= null)
label.setText(c);

CheckBox cb =(CheckBox)v.findViewById(R.id.check1);
//cb.setId(childPosition);
cb.setTag(groupPosition +child+ childPosition);
cb.setChecked(false);
if(AndroidClientEntity.objHasmap.get(groupPosition +child+ childPosition)!= null)
{
cb.setChecked(AndroidClientEntity.objHasmap.get(groupPosition +child+ childPosition)) ;
}
// cb.setOnCheckedChangeListener(this);
return v;
}

public int getChildrenCount(int groupPosition){
return child.get(groupPosition).size();
}

public Object getGroup(int groupPosition){

return groupNames.get(groupPosition);
}

public int getGroupCount(){
return groupNames.size();
}
public long getGroupId(int groupPosition){
return(long)(groupPosition * 1024); //与getChildId一致
}

public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent){
View v = null;
if(convertView!= null)
v = convertView;
else
v = inflater.inflate(R.layout.group_row,parent,false);
String gt =(String)getGroup(groupPosition);
TextView colorGroup =(TextView)v.findViewById(R.id.childname);
if(gt!= null)
colorGroup.setText(gt);
CheckBox cb =(CheckBox)v.findViewById(R.id.check2);
cb.setTag(groupPosition +cbgroup);
//Object_SecurityActivityobjHasmap.put(groupPosition+\"cbgroup,false);
cb.setChecked(false);
return v;
}

public boolean hasStableIds(){
return true;
}

public boolean isChildSelectable(int groupPosition,int childPosition){
Log.e(is group checked,group+ groupPosition);
Log.e(selectable,has+ childPosition);
return true;
}

public void onGroupCollapsed(int groupPosition){}
public void onGroupExpanded(int groupPosition){}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
// TODO自动生成方法存根



}
public void isChecked(){

}

}

并且Activity类别:



public class Object_SecurityActivity extends ExpandableListActivity implements OnClickListener {

@Override
public boolean onChildClick(ExpandableListView parent,View v,
int groupPosition,int childPosition,long id){
// TODO自动生成的方法存根
CheckBox cb =(CheckBox)v.findViewWithTag(groupPosition +child+ childPosition);
// Boolean state = objHasmap.get(groupPosition +child+ childPosition);
cb.toggle();
if(cb.isChecked())
{
AndroidClientEntity.objHasmap.put(groupPosition +child+ childPosition,true);

} else
{AndroidClientEntity.objHasmap.remove(groupPosition +child+ childPosition);

}

//在集合中添加值
//cb.setEnabled(state);
return super.onChildClick(parent,v,groupPosition,childPosition,id);
}



private AndroidClientEntity obj_android_client;
private static final String LOG_TAG =ElistCBox2;
private String username,password,clientname;

@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
//setContentView (R.layout.main);
Intent security_intent = getIntent();
String id = secure_intent.getStringExtra(id);

obj_android_client =(AndroidClientEntity)getApplicationContext();
username = obj_android_client.getUsername();
password = obj_android_client.getPassword();
clientname = obj_android_client.getClientName();
new Securityasync()。execute(username,password,clientname,id);

}

public boolean onCreateOptionsMenu(菜单菜单){
new MenuInflater(getApplication())。inflate(R.menu.context_menu_security,
menu );
return(super.onPrepareOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item){
// startDownload();
if(item.getItemId()== R.id.setsecurity){
// code here
Log.e(hiiiiiiiiiiiiiiii,scejkkl yu menu pressed);
}
return(super.onOptionsItemSelected(item));
}
类Securityisync扩展AsyncTask< String,String,String> {
String sesurity_response = null;
ProgressDialog对话框;
private Expandable_list_Adapter expListAdapter;

@Override
protected String doInBackground(String ... params){
if((isOnline(Object_SecurityActivity.this).equals(true))){
Security_service security_obj = new Security_service();
try {
sesurity_response = security_obj.getUsersRoles(params [0],
params [1],params [2],params [3]);
} catch(IllegalStateException e){
e.printStackTrace();
} catch(JSONException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
return null;
}

@Override
protected void onPostExecute(String result){
if(isOnline(Object_SecurityActivity.this).equals(true)){

setContentView(R.layout.layout_expandable_listview);
ArrayList< String> groupNames = new ArrayList< String>();
ArrayList< String> sub = new ArrayList< String>();
ArrayList< ArrayList< String>> child = new ArrayList< ArrayList< String>>();
ArrayList< String> sub_id = new ArrayList< String>();
ArrayList< String> objrid = new ArrayList< String>();
try {
JSONArray json = new JSONArray(ssurity_response);
// JSONArray json_child = new JSONArray(sesurity_response);
for(int i = 0; i JSONObject json_obj = json.getJSONObject(i);
if(json_obj.getString(PRid)。equalsIgnoreCase(0)){

String ObjectRid = json_obj.getString(ObjectRid);
int m = 0;
objrid.add(m,ObjectRid);
m ++;
groupNames.add(json_obj.getString(Name));
for(int j = 0; j< json.length(); j ++){
JSONObject json_child = json.getJSONObject(j);
if(ObjectRid.equalsIgnoreCase(json_child
.getString(PRid))){
int n = 0;
sub_id.add(n,json_child.getString(ObjectRid));
sub.add(json_child.getString(Name));
}

}
child.add(sub);

}

}
expListAdapter = new Expandable_list_Adapter(getBaseContext(),
groupNames,child);
setListAdapter(expListAdapter);
Log.e(size in error,son+ json.length());
} catch(JSONException e){
Log.e(,,e);
Toast.makeText(getBaseContext(),parsing error,
Toast.LENGTH_LONG).show();
}
Log.e(sizeof list,+ sub_id.size());
迭代器itr = objrid.iterator();


while(itr.hasNext()){
Log.e(id,value+ itr.next());
}

}
}

@Override
protected void onPreExecute(){
super.onPreExecute();

}
}
public void onClick(View v){
// TODO自动生成方法存根

}



}



感谢pallavi,amit rai和Photon 。


I have an expanded list view that have check boxes in parent nodes as well as child items. all data comes from web service so its dynamic.

Image attached:

Now on menu item click i want to fetch all check box status . please guide me how can i get id of check boxes used in it.

code attached:

/**
 * 
 */


    public class Object_SecurityActivity extends ExpandableListActivity {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            return super.onChildClick(parent, v, groupPosition, childPosition, id);
        }

        @Override
        public void onContentChanged() {
            // TODO Auto-generated method stub
            super.onContentChanged();
        }

        private AndroidClientEntity obj_android_client;
        private static final String LOG_TAG = "ElistCBox2";
        private String username, password, clientname;

        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            //setContentView(R.layout.main);
            Intent security_intent = getIntent();
            String id = security_intent.getStringExtra("id");
            obj_android_client = (AndroidClientEntity) getApplicationContext();
            username = obj_android_client.getUsername();
            password = obj_android_client.getPassword();
            clientname = obj_android_client.getClientName();
            new Securityasync().execute(username, password, clientname, id);

        }

        class Securityasync extends AsyncTask<String, String, String> {
            String sesurity_response = null;
            ProgressDialog dialog;
            private Expandable_list_Adapter expListAdapter;

            @Override
            protected String doInBackground(String... params) {
                if ((isOnline(Object_SecurityActivity.this).equals("true"))) {
                    Security_service security_obj = new Security_service();
                    try {
                        sesurity_response = security_obj.getUsersRoles(params[0],
                                params[1], params[2], params[3]);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                if (isOnline(Object_SecurityActivity.this).equals("true")) {

                    setContentView(R.layout.layout_expandable_listview);
                    ArrayList<String> groupNames = new ArrayList<String>();
                    ArrayList<String> sub = new ArrayList<String>();
                    ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>();
                    ArrayList<String> sub_id = new ArrayList<String>();
                    ArrayList<String> objrid = new ArrayList<String>();
                    try {
                        JSONArray json = new JSONArray(sesurity_response);
                        // JSONArray json_child=new JSONArray(sesurity_response);
                        for (int i = 0; i < json.length(); i++) {
                            JSONObject json_obj = json.getJSONObject(i);
                            if (json_obj.getString("PRid").equalsIgnoreCase("0")) {

                                String ObjectRid = json_obj.getString("ObjectRid");
                                int m=0;
                                objrid.add(m,ObjectRid);
                                m++;
                                groupNames.add(json_obj.getString("Name"));
                                for (int j = 0; j < json.length(); j++) {

                                    JSONObject json_child = json.getJSONObject(j);
                                    if (ObjectRid.equalsIgnoreCase(json_child
                                            .getString("PRid"))) {
                                        int n=0;
                                        sub_id.add(n,json_child.getString("ObjectRid"));
                                        sub.add(json_child.getString("Name"));
                                    }

                                }
                                child.add(sub);

                            }

                        }
                        expListAdapter = new Expandable_list_Adapter(getBaseContext(),
                                groupNames, child);
                        setListAdapter(expListAdapter);
                        Log.e("size in error", "son " + json.length());
                    } catch (JSONException e) {
                        Log.e("", "", e);
                        Toast.makeText(getBaseContext(), "parsing error",
                                Toast.LENGTH_LONG).show();
                    }
                    Log.e("sizeof list", ""+sub_id.size());
                    Iterator itr=objrid.iterator();


                    while(itr.hasNext()){
                        Log.e("id","value "+itr.next());
                    }

                }
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }
        }

    }

And adapter class:

public class Expandable_list_Adapter extends BaseExpandableListAdapter implements OnCheckedChangeListener {

    private Context context;
    private ArrayList<String> groupNames;
    private ArrayList<ArrayList<String>> child;
    private LayoutInflater inflater;

    public Expandable_list_Adapter(Context context, 
                        ArrayList<String> groupNames,
                        ArrayList<ArrayList<String>> child ) { 
        this.context = context;
        this.groupNames= groupNames;
        this.child = child;
        inflater = LayoutInflater.from( context );
    }

    public Object getChild(int groupPosition, int childPosition) {
        return child.get( groupPosition ).get( childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_row, parent, false); 
       String c = (String)getChild( groupPosition, childPosition );
        TextView color = (TextView)v.findViewById( R.id.childname );
        if( color != null )
            color.setText( c );

        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );

        //cb.setChecked(false);
        cb.setOnCheckedChangeListener(this);
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return child.get( groupPosition ).size();
    }

    public Object getGroup(int groupPosition) {
        return groupNames.get( groupPosition );        
    }

    public int getGroupCount(){
         return groupNames.size();
    }
    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
        TextView colorGroup = (TextView)v.findViewById( R.id.childname );
        if( gt != null )
            colorGroup.setText( gt );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check2 );
        cb.setChecked(false);
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.e("is group checked","group "+groupPosition);
        Log.e("selectable","has" +childPosition);
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {}
    public void onGroupExpanded(int groupPosition) {}

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub

    }
public void isChecked(){

}

}

Child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
      android:background="#21415A"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/childname"
         android:paddingLeft="50px"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

    <TextView android:id="@+id/rgb"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="100px"
         android:layout_height="wrap_content"/>

    <CheckBox
        android:id="@+id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false" />

</RelativeLayout>

parent.xml is same as child.

please guide me how can i get ids of check boxes because on menu item selected i have to do operation basics on that.

Edit: i tried to setTag() and getTag() . but now expandable list view is showing strange behavior. when i select an checkbox and expand another group all chekbok boxes is set to default. What i have to do in this case. i dont know the resion why it is not saving state. i am faceing same problem as Strange behaviour in Expandablelistview - Android Plz guide

解决方案

finally solved my problem. Adapter class:

 public class Expandable_list_Adapter extends BaseExpandableListAdapter implements OnCheckedChangeListener {

    private Context context;
    private ArrayList<String> groupNames;
    private ArrayList<ArrayList<String>> child;
    private LayoutInflater inflater;

    public Expandable_list_Adapter(Context context, 
                        ArrayList<String> groupNames,
                        ArrayList<ArrayList<String>> child ) { 
        AndroidClientEntity.objHasmap.clear();
        this.context = context;
        this.groupNames= groupNames;
        this.child = child;
        inflater = LayoutInflater.from( context );
    }

    public Object getChild(int groupPosition, int childPosition) {
        return child.get( groupPosition ).get( childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
             v = inflater.inflate(R.layout.child_row, parent, false); 
       String c = (String)getChild( groupPosition, childPosition );
       Log.e("hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii",c);

        TextView label = (TextView)v.findViewById( R.id.childname );
        if( label != null )
            label.setText( c );

        CheckBox cb = (CheckBox)v.findViewById( R.id.check1);
        //cb.setId(childPosition);
        cb.setTag(groupPosition+"child"+childPosition);
        cb.setChecked(false);
        if(AndroidClientEntity.objHasmap.get(groupPosition+"child"+childPosition)!=null)
        {
        cb.setChecked(AndroidClientEntity.objHasmap.get(groupPosition+"child"+childPosition));
        }
    //    cb.setOnCheckedChangeListener(this);
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return child.get( groupPosition ).size();
    }

    public Object getGroup(int groupPosition) {

        return groupNames.get( groupPosition );        
    }

    public int getGroupCount(){
         return groupNames.size();
    }
    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
        TextView colorGroup = (TextView)v.findViewById( R.id.childname );
        if( gt != null )
            colorGroup.setText( gt );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check2 );
        cb.setTag(groupPosition+"cbgroup");
        //Object_SecurityActivityobjHasmap.put(groupPosition+"cbgroup", false);
        cb.setChecked(false);
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.e("is group checked","group "+groupPosition);
        Log.e("selectable","has" +childPosition);
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {}
    public void onGroupExpanded(int groupPosition) {}

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub



    }
public void isChecked(){

}

}

And Activity class:

public class Object_SecurityActivity extends ExpandableListActivity implements OnClickListener{

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    // TODO Auto-generated method stub
    CheckBox cb=(CheckBox) v.findViewWithTag(groupPosition+"child"+childPosition);
//Boolean state=objHasmap.get(groupPosition+"child"+childPosition);
    cb.toggle();
    if(cb.isChecked())
    {
        AndroidClientEntity.objHasmap.put(groupPosition+"child"+childPosition,true );

    }else
    {AndroidClientEntity.objHasmap.remove(groupPosition+"child"+childPosition);

    }

    //add value in set
//cb.setEnabled(state);
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}



private AndroidClientEntity obj_android_client;
private static final String LOG_TAG = "ElistCBox2";
private String username, password, clientname;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //setContentView(R.layout.main);
    Intent security_intent = getIntent();
    String id = security_intent.getStringExtra("id");

    obj_android_client = (AndroidClientEntity) getApplicationContext();
    username = obj_android_client.getUsername();
    password = obj_android_client.getPassword();
    clientname = obj_android_client.getClientName();
    new Securityasync().execute(username, password, clientname, id);

}

public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(getApplication()).inflate(R.menu.context_menu_security,
            menu);
    return (super.onPrepareOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item) {
    // startDownload();
    if (item.getItemId() == R.id.setsecurity) {
        //code here 
        Log.e("hiiiiiiiiiiiiiiii","scejkkl yu menu pressed");
         }
    return (super.onOptionsItemSelected(item));
}
class Securityasync extends AsyncTask<String, String, String> {
    String sesurity_response = null;
    ProgressDialog dialog;
    private Expandable_list_Adapter expListAdapter;

    @Override
    protected String doInBackground(String... params) {
        if ((isOnline(Object_SecurityActivity.this).equals("true"))) {
            Security_service security_obj = new Security_service();
            try {
                sesurity_response = security_obj.getUsersRoles(params[0],
                        params[1], params[2], params[3]);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (isOnline(Object_SecurityActivity.this).equals("true")) {

            setContentView(R.layout.layout_expandable_listview);
            ArrayList<String> groupNames = new ArrayList<String>();
            ArrayList<String> sub = new ArrayList<String>();
            ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>();
            ArrayList<String> sub_id = new ArrayList<String>();
            ArrayList<String> objrid = new ArrayList<String>();
            try {
                JSONArray json = new JSONArray(sesurity_response);
                // JSONArray json_child=new JSONArray(sesurity_response);
                for (int i = 0; i < json.length(); i++) {
                    JSONObject json_obj = json.getJSONObject(i);
                    if (json_obj.getString("PRid").equalsIgnoreCase("0")) {

                        String ObjectRid = json_obj.getString("ObjectRid");
                        int m=0;
                        objrid.add(m,ObjectRid);
                        m++;
                        groupNames.add(json_obj.getString("Name"));
                        for (int j = 0; j < json.length(); j++) {
                            JSONObject json_child = json.getJSONObject(j);
                            if (ObjectRid.equalsIgnoreCase(json_child
                                    .getString("PRid"))) {
                                int n=0;
                                sub_id.add(n,json_child.getString("ObjectRid"));
                                sub.add(json_child.getString("Name"));
                            }

                        }
                        child.add(sub);

                    }

                }
                expListAdapter = new Expandable_list_Adapter(getBaseContext(),
                        groupNames, child);
                setListAdapter(expListAdapter);
                Log.e("size in error", "son " + json.length());
            } catch (JSONException e) {
                Log.e("", "", e);
                Toast.makeText(getBaseContext(), "parsing error",
                        Toast.LENGTH_LONG).show();
            }
            Log.e("sizeof list", ""+sub_id.size());
            Iterator itr=objrid.iterator();


            while(itr.hasNext()){
                Log.e("id","value "+itr.next());
            }

        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }
}
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

Thanks to pallavi,amit rai and Photon.

这篇关于如何获取扩展列表视图项目中的复选框的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆