2

這是我正在使用的:我有一個ExpandableListView使用SimpleCursorTreeAdapter從SQLite數據庫填充視圖。我正在使用自定義組和子視圖,而組視圖和子視圖中都有按鈕。我需要知道如何在這些按鈕上設置按鈕偵聽器,以便在調用偵聽器時知道它來自哪個行(或光標ID)。我能夠設置一個工作偵聽器onChildItemDetailsClicked(),但在偵聽器中,我不知道哪一行對應按鈕按下。有任何想法嗎?單擊Listener的ExpandableListView子視圖SimpleCursorTreeAdapter

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

我在做類似的東西這裏http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42 2012-05-16 05:16:04

回答

2

設置ViewBinder到您的適配器和那裏,爲按鈕添加監聽器。 this可以提供幫助。

+1

基於@ Keyhan的幫助,我得到了這個工作,通過添加一個類MyViewBinder實現了android.widget.SimpleCursorTreeAdapter .ViewBinder'。我重寫'public boolean setViewValue(View view,Cursor cursor,int columnIndex)',並在該方法中設置我的視圖並使用'view.setTag(myId)'將id與視圖關聯。 – mkasberg 2012-03-18 18:00:23

+0

我想要相同,但我使用BaseExpandableListAdapter(因爲我不想使用數據庫或光標)..請告訴我如何做到這一點? – Shruti 2012-11-01 10:43:11

+0

我認爲這可能有所幫助:http://www.vogella.com/code/de.vogella.android.apitest/src/com/example/android/apis/view/ExpandableList1.html – 2012-11-01 19:17:06

相關問題