다음은 각 그룹에 체크 박스와 텍스트보기가있는 확장 가능한 목록보기의 코드입니다. 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>
내 질문에 onClick 메서드에서 자식보기의 개체를 수정할 수 있지만 변경 내용이 반영되지 않습니다. – gaurav24
내보기 변경 사항이 내 데이터에 반영되지 않는다면 데이터가 아닌보기가 수정되므로 정상입니다. 단순화하기 위해 어댑터는 변환 할 데이터를 표시하므로 notifyDataSetChanged()를 호출하면 데이터를 가져 와서 (어댑터에 대해 로컬) 뷰를 반전하지 않고 채 웁니다. 자신 만의 어댑터를 만들려면 간단하고 효율적인 방법을 사용하고 코드를 유지하려면 checkBox보기 (수신기 설정)와 데이터 연결을 수행해야합니다 (그러나이 방법은 확실하지 않습니다.). – OBuiron
getChild()에서; CheckBox cb = (CheckBox) convertView.findViewById (R.id.cb_child); CheckBox를 사용하여 check를 설정하거나 "if (checkAll)"checkAll이 setter에 의해 설정된 속성 (activity)을 호출 한 다음 notifyDataSetChanged()를 호출하여 즐기는 것과 같은 조건) – OBuiron