2010-08-10 79 views
1

我一直在玩下面的Android应用程序示例。SimpleCursorTreeAdapter - 如何自定义子布局

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList2.html

我能够触发动作,当一个孩子被点击但是我似乎无法弄清楚 如何: 1.自定义每个孩子的标签。 2.改变每个孩子的外貌。 (例如,我希望每个孩子都能显示电话号码,然后在右侧显示两个图形按钮,每个按钮都会绑定一个自定义操作。)

任何代码或链接都将不胜感激。

+0

我正在做类似的东西这里http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42 2012-05-16 07:32:14

+0

示例代码的链接已损坏。该代码现在位于https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList2.java – LarsH 2016-11-29 11:07:35

回答

1

一个可能的解决方案可能是使用setViewBinder来设置SimpleCursorTreeAdapter.ViewBinder,这与您对SimpleCursorAdapter非常相似。

我不敢肯定这会起作用,因为我没有尝试过,但它似乎是相似的。我试图解决同样的问题,所以如果它不起作用,我会尽力在这里发布。

+0

对,所以我已经了解到:SimpleCursorTreeAdapter.ViewBinder是在API级别5中引入的。我瞄准的是4级设备。 :/ – 2010-08-17 11:50:59

+0

我是Android的新手,我真正在努力研究如何“接管”创建子项,以便我可以分配布局并以编程方式添加元素。我能够仅使用XML来改变每个孩子的外观,但是当涉及处理事件和分配动态属性时,我几乎要跺脚。 – user412164 2010-08-17 17:46:46

1

你可以在你的SimpleCursorTreeAdapter中覆盖“bindChildView”。这就是我做的:

 class MyAdapter extends SimpleCursorTreeAdapter { 

     @Override 
    protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) { 
     // TODO Auto-generated method stub 
     super.bindChildView(view, context, cursor, isLastChild); 

     String title = cursor.getString(cursor.getColumnIndex(Alert.COL_DAY));    
     ((TextView)view.findViewById(R.id.child_day)).setText(title);   
    } 

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

     protected Cursor getChildrenCursor(Cursor groupCursor) { 
      int idColumn = groupCursor.getColumnIndex(Pill.COL_ID); 
      return Alert.list(db, groupCursor.getInt(idColumn), null, Alert.COL_DAY); 
     } 

    } 

不过,你必须提供childFrom和childTo PARAMS创建时,它给适配器,所以它会在bindChildView使用它们。