想了几天,布局填充2个一样的分类,在 viewBinder 里把 adapter 子类填充抄过来取数字就行了。
ExpandableListView + SimpleCursorTreeAdapter_海天鹰的博客-CSDN博客
group_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/group_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
android:text="group_name"
android:textAppearance="?android:attr/textAppearanceListItem" />
<TextView
android:id="@+id/group_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="0" />
</LinearLayout>
DBHelper helper = new DBHelper(this);
Cursor groupCursor = helper.category();
// android.R.layout.simple_expandable_list_item_1 -> R.layout.group_row
// android.R.id.text1 -> R.id.group_name, R.id.group_count
SimpleCursorTreeAdapter adapter = new MySimpleCursorTreeAdapter(groupCursor, this, R.layout.group_row, R.layout.favorite_row, new String[] { "category", "category" }, new int[] { R.id.group_name, R.id.group_count }, new String[] { "_id", "title", "website", "category", "website" }, new int[] { R.id.id, R.id.title, R.id.website, R.id.category, R.id.imageView_favicon });
adapter.setViewBinder(new SimpleCursorTreeAdapter.ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if (view.getId() == R.id.group_count) {
TextView textView = (TextView) view;
String s = cursor.getString(columnIndex);
DBHelper helper = new DBHelper(FavoriteActivity1.this);
Cursor cursor1 = helper.queryCategory(s);
int count = cursor1.getCount();
textView.setText("" + count);
return true;
}
return false;
}
});
expandableListView.setAdapter(adapter);