Klicken Sie in der untergeordneten Ansicht „ExpandableListView“ mit SimpleCursorTreeAdapter auf die Schaltfläche „Listener für“.

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

Frage

Hier ist, womit ich arbeite:Ich habe eine ExpandableListView, die einen SimpleCursorTreeAdapter verwendet, um die Ansicht aus einer SQLite-Datenbank zu füllen.Ich verwende benutzerdefinierte Gruppen- und untergeordnete Ansichten und sowohl die Gruppen- als auch die untergeordneten Ansichten verfügen über Schaltflächen.Ich muss wissen, wie ich einen Schaltflächen-Listener für diese Schaltflächen festlegen kann, damit ich beim Aufruf des Listeners weiß, aus welcher Zeile (oder Cursor-ID) er stammt.Ich kann einen funktionierenden Listener für „onChildItemDetailsClicked()“ festlegen, aber im Listener weiß ich nicht, welche Zeile dem Tastendruck entspricht.Irgendwelche Ideen?

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();
    }
}

Gruppenansicht:

       <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>

Untergeordnete Ansicht:

    <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>
War es hilfreich?

Lösung

Satz ViewBinder zu Ihrem Adapter und fügen Sie dort einen Listener für Schaltflächen hinzu.Das kann helfen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top