2014-11-27 2 views
0

다음은 각 그룹에 체크 박스와 텍스트보기가있는 확장 가능한 목록보기의 코드입니다. Select All (모두 선택) 체크 박스가 있습니다. 클릭하면 모든 하위보기의 확인란의 상태가 변경되지만 변경되지는 않습니다. 누군가 설명 할 수 있습니까 ??하위보기 항목이 새로 고침되지 않습니다.

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener { 

    ExpandableListAdapter listAdapter; 
     ExpandableListView expListView; 
     List<String> listDataHeader; 
     static CheckBox selectAll; 
     HashMap<String, List<String>> listDataChild; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      // get the listview 
      expListView = (ExpandableListView) findViewById(R.id.expandableListView1); 
      selectAll=(CheckBox)findViewById(R.id.checkBox1); 
      // preparing list data 
      prepareListData(); 

      listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

      // setting list adapter 
      expListView.setAdapter(listAdapter); 
      selectAll.setOnClickListener(this); 

     } 

     /* 
     * Preparing the list data 
     */ 
     private void prepareListData() { 
      listDataHeader = new ArrayList<String>(); 
      listDataChild = new HashMap<String, List<String>>(); 

      // Adding child data 
      listDataHeader.add("Top 250"); 
      listDataHeader.add("Now Showing"); 
      listDataHeader.add("Coming Soon.."); 

      // Adding child data 
      List<String> top250 = new ArrayList<String>(); 
      top250.add("The Shawshank Redemption"); 
      top250.add("The Godfather"); 

      List<String> nowShowing = new ArrayList<String>(); 
      nowShowing.add("The Conjuring"); 
      nowShowing.add("Despicable Me 2"); 


      List<String> comingSoon = new ArrayList<String>(); 
      comingSoon.add("2 Guns"); 
      comingSoon.add("The Smurfs 2"); 


      listDataChild.put(listDataHeader.get(0), top250); // Header, Child data 
      listDataChild.put(listDataHeader.get(1), nowShowing); 
      listDataChild.put(listDataHeader.get(2), comingSoon); 
     } 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      CheckBox cb=(CheckBox)listAdapter.getChildView(0, 0, false, null,   null).findViewById(R.id.cb_child); 
      cb.setChecked(true); 
      ((BaseExpandableListAdapter) listAdapter).notifyDataSetChanged(); 
     } 

} 

ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    private HashMap<String, List<String>> _listDataChild; 


    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 

    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_layout, null); 
     } 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_layout, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.textView1); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.expandablelisttest.MainActivity" 
    tools:ignore="MergeRootFrame" > 

    <ExpandableListView 
     android:layout_marginTop="100dp" 
     android:id="@+id/expandableListView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" > 

    </ExpandableListView> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="21dp" 
     android:text="Select All" /> 

</RelativeLayout> 

당신의 ExpandableListAdapter이 항목 만 문자열이 아닌 확인란을 알고 있기 때문에 child_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <CheckBox 
     android:id="@+id/cb_child" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="18dp" 
     android:text="CheckBox" 
     android:checked="false"/> 

</RelativeLayout> 

group_layout.xml는

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="18dp" 
     android:layout_marginTop="14dp" 
     android:text="TextView" /> 

</RelativeLayout> 

답변

0

메서드 getChildView (..)는 어댑터에서 그룹 아래의 확장 가능한 하위보기를 채우는 데 사용됩니다.

나는에 대한 자세한 설명은, finnaly

그리고 (당신이 1 그룹 = 1 개 항목이있는 경우, 당신은 단순히 자신의 POJO 객체 모델 만 1 ArrayList를 사용할 수 있습니다) BaseExpandableListAdapter에서 확장 자신의 ExpandableListAdapter를 작성하는 것이 좋습니다 수 있습니다 notifyDataSetChanged()를 호출하면 목록의 새로 고침 뷰가 어댑터의 데이터로 표시됩니다 (예 : 각 부울 값 가져 오기 또는 체크 박스에 값 사용)

+0

내 질문에 onClick 메서드에서 자식보기의 개체를 수정할 수 있지만 변경 내용이 반영되지 않습니다. – gaurav24

+0

내보기 변경 사항이 내 데이터에 반영되지 않는다면 데이터가 아닌보기가 수정되므로 정상입니다. 단순화하기 위해 어댑터는 변환 할 데이터를 표시하므로 notifyDataSetChanged()를 호출하면 데이터를 가져 와서 (어댑터에 대해 로컬) 뷰를 반전하지 않고 채 웁니다. 자신 만의 어댑터를 만들려면 간단하고 효율적인 방법을 사용하고 코드를 유지하려면 checkBox보기 (수신기 설정)와 데이터 연결을 수행해야합니다 (그러나이 방법은 확실하지 않습니다.). – OBuiron

+0

getChild()에서; CheckBox cb = (CheckBox) convertView.findViewById (R.id.cb_child); CheckBox를 사용하여 check를 설정하거나 "if (checkAll)"checkAll이 setter에 의해 설정된 속성 (activity)을 호출 한 다음 notifyDataSetChanged()를 호출하여 즐기는 것과 같은 조건) – OBuiron

0

당신은이 방법을 수행 할 수 없습니다.
ExpandableListAdapter를 수정하여 그 안에있는 확인란에 액세스 할 수 있어야합니다.

--->http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html이 도움이 될 수 있습니다.
목록의 각 항목에 원하는 개체가 포함 된 구조를 만들어야합니다.

+0

이것은 대답이 아니라 주석입니다. – Naruto

+0

가져 오지 않았습니다. ... Log.e ("State", ""+ cb_child.isChecked());를 사용하여 체크 박스의 상태를 인쇄 할 때와 비슷합니다. 올바른 로그를 얻고 있습니다. 즉, 모두 선택만으로 상태가 전환되지만 표시되지 않습니다. – gaurav24

관련 문제