SimpleCursortReadapterを使用して、ExpandableListView子ビューの[Listener for]ボタンをクリックします。

StackOverflow https://stackoverflow.com//questions/9661493

質問

ここに取り組んでいるものです.SimpleCursorTreadapterを使用してSQLiteデータベースからビューを入力するExpandableListViewがあります。カスタムグループと子ビューを使用しています。グループと子ビューは両方ともボタンを持っています。リスナーが呼び出されたときに起こる行(またはカーソルID)を知っているように、これらのボタンでボタンリスナーを設定できる方法を知っておく必要があります。onchildItemdetailsClicked()を設定することができますが、リスナーの内側にはボタンを押すかがわかりません。任意のアイデア?

public class MyListActivity extends ExpandableListActivity
{   
private DatabaseHelper mDbHelper;
private Cursor mGroupCursor;
private int mGroupIdColumnIndex;
private ExpandableListAdapter mAdapter;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        mDbHelper = new DatabaseHelper(this);
        mDbHelper.openDatabase();

        this.getExpandableListView().setItemsCanFocus(true);
        fillData();
    }

    private void fillData() 
    {
        mGroupCursor = mDbHelper.getAllGroupsCursor(); // fills cursor with list of top nodes - groups 
        startManagingCursor(mGroupCursor);

        // Cache the ID column index
        mGroupIdColumnIndex = mGroupCursor.getColumnIndex(DatabaseHelper.COL_ID);

        // Set up our adapter
        String[] groupCols = new String[] { DatabaseHelper.COL_TITLE, DatabaseHelper.COL_TEXT };
        int[] groupViews = new int[] { R.id.group_title, R.id.group_text };
        String[] childCols = new String[] {DatabaseHelper.COL_TEXT };
        int[] childViews = new int[] {R.id.child_text };

        mAdapter = new ExamineActivityAdapter(this, mGroupCursor, 
            R.layout.mylist_group_row, groupCols, groupViews,
            R.layout.mylise_child_row, childCols, childViews);

        setListAdapter(mAdapter);
}


    @Override
    public void onDestroy()
    {
        mDbHelper.close();
        super.onDestroy();
    }

    public class MyListActivityAdapter extends SimpleCursorTreeAdapter
    {   
        public MyListActivityAdapter(Context context, Cursor cursor, 
            int groupLayout, String[] groupFrom, int[] groupTo, 
            int childLayout, String[] childFrom, int[] childTo) 
        {
        super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
        }

        // returns cursor with subitems for given group cursor
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) 
        {
        Cursor childCursor = mDbHelper.getChildrenCursorForGroup(groupCursor.getInt(mGroupIdColumnIndex));
        startManagingCursor(childCursor);
            return childCursor;
        }
    }

    public void onChildItemDetailsClicked(View v)
    {       
        Toast toast = Toast.makeText(this, "child detail clicked.", Toast.LENGTH_SHORT);

        toast.show();
    }
}
.

グループビュー:

       <TextView
           android:id="@+id/group_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center_vertical"
           android:textSize="20sp"
           android:singleLine="true"
       />

       <Button 
           android:id="@+id/group_detail_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:lines="1"
           android:textSize="12sp"
           android:text="@string/button_detail"
           android:focusable="false"
           android:layout_alignParentRight="true"
           android:layout_below="@id/group_title"
           android:layout_marginLeft="10dip" />

       <TextView
           android:id="@+id/group_text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:ellipsize="marquee"
           android:textSize="12sp"
           android:lines="3"
           android:layout_toLeftOf="@id/group_detail_button"
           android:layout_alignTop="@id/group_detail_button"
       />

</RelativeLayout>
.

子ビュー:

    <TextView android:id="@+id/child_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:gravity="center_vertical" />

    <Button android:id="@+id/child_detail_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:lines="1"
           android:textSize="12sp"
           android:text="@string/button_detail"
           android:focusable="false"
           android:onClick="onChildItemDetailsClicked"
           android:layout_alignParentRight="true"
           android:layout_below="@id/child_text"
           android:layout_marginLeft="10dip" />

    <CheckBox android:id="@+id/child_check"
        android:focusable="false"
        android:text="@string/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/child_text"
        android:layout_toLeftOf="@id/child_detail_button"
        android:layout_alignTop="@id/child_detail_button" />

</RelativeLayout>
.

役に立ちましたか?

解決

ViewBinderをアダプタに設定し、そこに、ボタンのリスナーを追加します。 このは役に立ちます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top