2012-04-11 34 views
5

Tôi có chế độ xem danh sách được mở rộng có các hộp kiểm trong các nút cha cũng như các mục con. tất cả dữ liệu đến từ dịch vụ web nên năng động của nó.cách lấy id của các hộp kiểm trong mục xem danh sách mở rộng?

Hình ảnh kèm theo:

enter image description here

Bây giờ trên menu nhấp tôi muốn lấy tất cả trạng thái hộp kiểm. xin vui lòng hướng dẫn tôi làm thế nào tôi có thể nhận được id của hộp kiểm được sử dụng trong nó.

mã kèm theo:

/** 
* 
*/ 


    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(); 

      } 
     } 

    } 

Và bộ chuyển đổi lớp:

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 là giống như đứa trẻ.

hãy hướng dẫn tôi làm cách nào tôi có thể nhận id của hộp kiểm vì trên mục menu được chọn, tôi phải thực hiện các thao tác cơ bản về điều đó.

Chỉnh sửa: tôi đã cố gắng setTag() và getTag(). nhưng giờ đây, chế độ xem danh sách có thể mở rộng đang hiển thị hành vi lạ. khi tôi chọn một hộp kiểm và mở rộng một nhóm khác, tất cả các hộp chekbok được đặt thành mặc định. Những gì tôi phải làm trong trường hợp này. tôi không biết resion tại sao nó không phải là tiết kiệm nhà nước. tôi đang faceing cùng một vấn đề như Strange behaviour in Expandablelistview - Android Plz hướng dẫn

Trả lời

1

cuối cùng đã giải quyết được sự cố của tôi. Adaptor lớp:

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(){ 

} 

} 

Và Hoạt động lớp:

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 

} 

}

Nhờ Pallavi, amit rai và Photon.

+1

tốt để xem giải quyết vấn đề của riêng mình – Trikaldarshi

+0

công việc tốt @Rajiv –

+0

cảm ơn @Photon và Avi –

1

thực tế những gì bao giờ kiểm soát bạn thêm vào danh sách hoặc lưới đó được sao chép vào thời gian chạy có cùng id như bạn khai báo cho một tại thời điểm thiết kế

cho ví dụ : trong bạn là trường hợp đây là những childname, rgb, Check1

vì vậy nếu bạn cần phải biết về những hộp duy nhất bạn nên đi cho từng người một sử dụng cho vòng lặp

và nếu bạn muốn làm một số điều tại sao don Bạn không làm điều này

về sự kiện kiểm tra hộp nhấp chuột

public void onClickevent(View v) 
{ 
private CheckBox ch = (CheckBox) findViewById(v.getId()); 
    if (ch.IsChecked){ 
    //do some thing 
    } 
} 

hoặc cách khác bạn nên đi với cách của tôi thêm một lĩnh vực độc đáo như

<TextView 
     android:id="@+id/txtComp1_ID" 
     android:layout_width="0sp" 
     android:layout_height="40sp" 
     android:textSize="1pt" > 
    </TextView> 

vì nó có zero chiều rộng nên không thể nhìn thấy nhưng nó giữ id của hàng đó

hiện đang chạy vòng lặp for cho tất cả các mục trong chế độ xem danh sách

findview bạn kiểm tra hộp và sau đó tìm mẹ của nó và sau đó một lần nữa con (xem văn bản) của mình rằng id lĩnh vực này vì vậy bây giờ bạn có id và kiểm tra tình trạng kiểm tra hộp như trước cách này bạn có thể có được điều này

Xem ví dụ

v = (LinearLayout) findViewById(R.id.LinearNotebookList); 
      TextView _id, ID, Name; 
      for (int i = 0; i < nCount; i++) { 
       v = (LinearLayout) NotebookList.getAdapter().getView(i, null, 
         null); 
       _id = (TextView) v.findViewById(R.id.txt_Notebook_ID); 

       Available = (CheckBox) v 
         .findViewById(R.id.chkboxNotebookAvailable); 
       Display = (CheckBox) v.findViewById(R.id.chkboxNotebookDisplay); 
       Demo = (CheckBox) v.findViewById(R.id.chkboxNotebookDemo); 
       str_id = (String) _id.getText(); 

       if (Available.isChecked()) { 
        strAvailable = "YES"; 
       } else { 
        strAvailable = "NO"; 
       } 
       if (Display.isChecked()) { 
        strDisplay = "YES"; 
       } else { 
        strDisplay = "NO"; 
       } 
       if (Demo.isChecked()) { 
        strDemo = "YES"; 
        } else { 
         strDemo = "NO"; 
        } 
} 

bạn có thể thay đổi nó theo nhu cầu của bạn

+0

tôi đã thử cách tiếp cận của bạn. nhưng tôi chỉ nhận được một id những gì tôi đã xác định trong xml. nhưng tôi phải lấy giá trị hộp kiểm tra trên mục trình đơn chọn. Vấn đề ở đâu và làm thế nào tôi có thể nhận được tất cả các hộp kiểm tra tham khảo. –

+0

vâng tôi đã nói với bạn rằng bạn sẽ luôn có được một id duy nhất ứng dụng của tôi đang hoạt động tốt và phương pháp tốt nhất để giải quyết vấn đề này cho tôi biết cách tiếp cận bạn đang áp dụng ở đây để tôi cố gắng giải quyết nó – Trikaldarshi

1

Chỉ cần sử dụng setTag (int) và phương pháp getTag() để thiết lập id và nhận được id của điều khiển. như EditText, hộp vv

+0

không có thiết lập và nhận thẻ không phải là số – Trikaldarshi

1

sử dụng

cb.setId(groupPosition+childPosition); 

để thiết lập ID và để lấy sử dụng:

cb.getId(); 

nhóm vị trí + con vị trí sẽ cung cấp cho bạn các vị trí chính xác của mặt hàng đó.

và trong onClick, bạn có thể nhận ID bạn đã đặt trong khi tạo hộp kiểm.

+0

của id nơi tôi nên thêm: cb.setId (groupPosition + childPosition); –

+0

sau khi bạn tạo hộp kiểm "CheckBox cb = (CheckBox) v.findViewById (R.id.check1);" – Pallavi

+0

ok nhưng trong trường hợp id nhóm là 1 và id id con 2 và id nhóm là 2 và trẻ em là 1 trong trường hợp thuê, nó sẽ là xung đột –

Các vấn đề liên quan