如何向SimpleExpendableListAdapter添加按钮

时间:2015-11-10 12:19:03

标签: android

我已经完全使用了ExpendableListView,但是我想为组/子项添加按钮,因为我的ExpendableListView正在使用DB,我想为每个项添加CRUD。我使用SimpleExpendableListAdapter。

我在自定义MySimpleExpandableListAdapter中覆盖了这些方法。我知道,我需要以某种方式覆盖getGroupView和getChildView方法,但我如何覆盖它们以添加带onClickListener的按钮?

public MySimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo) {
    super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    return super.getGroupView(groupPosition, isExpanded, convertView, parent);
}

这是我的xml fragment_layout文件,带有我的消耗列表

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="stickeyd.teacherhelper.MainActivityPupilFragment">

<!-- TODO: Update blank fragment layout -->


<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

<ExpandableListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/expandableListView"
    android:layout_gravity="center" />

这是我使用RelativeLayout的主要片段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:showIn="@layout/activity_main"
tools:context=".MainActivityFragment">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="50dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"
    android:background="#dddddd" />

我的适配器初始化

    DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance(context);
    SQLiteDatabase mDatabase = mDatabaseHelper.getReadableDatabase();

    Cursor cursor = mDatabase.query(DatabaseHelper.TABLE_CLASS, new String[]{
                    DatabaseHelper._ID, DatabaseHelper.CLASS_TITLE, DatabaseHelper.CLASS_TEACHER, DatabaseHelper.CLASS_DESCRIPTION}, null,
            null,
            null,
            null,
            null
    );

    Map<String, String> map;
    ArrayList<Map<String, String>> groupDataList = new ArrayList<>();

    while (cursor.moveToNext()) {
        map = new HashMap<>();
        map.put("groupName",cursor.getString(cursor.getColumnIndex(DatabaseHelper.CLASS_TITLE)));
        groupDataList.add(map);
    }

    cursor.close();

    String groupFrom[] = new String[] { "groupName" };

    int groupTo[] = new int[] { android.R.id.text1 };

    ArrayList<ArrayList<Map<String, String>>> сhildDataList = new ArrayList<>();

    for (Map object: groupDataList) {

        String group = object.get("groupName").toString();
        String query = "SELECT * FROM " + DatabaseHelper.TABLE_PUPIL + " WHERE " +
                DatabaseHelper.PUPIL_CLASS + " = '" + group + "'"   ;

        cursor = mDatabase.rawQuery(query,null);

        ArrayList<Map<String, String>> сhildDataItemList = new ArrayList<>();

        while (cursor.moveToNext()) {
            String firstName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PUPIL_FIRSTNAME));
            map = new HashMap<>();
            map.put("firstName", firstName);
            сhildDataItemList.add(map);
        }
        cursor.close();
        сhildDataList.add(сhildDataItemList);
    }

    String childFrom[] = new String[] { "firstName" };
    int childTo[] = new int[] { android.R.id.text1 };

    return new MySimpleExpandableListAdapter(
            context, groupDataList,
            android.R.layout.simple_expandable_list_item_1, groupFrom,
            groupTo, сhildDataList, android.R.layout.simple_list_item_1,
            childFrom, childTo);

}

@Override
protected void onPostExecute(SimpleExpandableListAdapter adapter) {
    super.onPostExecute(adapter);
    MyBus.getInstance().post(new ExpListAsyncTaskEvent(adapter));

}

我需要为我的ExpendableList的每个组项添加按钮,也许我可以用我的按钮以组合视图的方式添加自定义布局?那对我来说是最好的解决方案

谢谢

1 个答案:

答案 0 :(得分:0)

要向groupView添加按钮,您可以执行以下操作:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    TextView v = (TextView)super.getGroupView(groupPosition, isExpanded, convertView, parent);
    LinearLayout ll = new LinearLayout(v.getContext());
    ll.addView(v);
    ll.addView(new Button(v.getContext()));
    return ll;
}

这应该在您的视图中添加一个0,0的空按钮(当您的行内容是一个简单的TextView时)。

希望这有帮助。