在Android应用程序中,如何向CursorTreeAdapter中的每个组添加一个按钮(它将打开对话框菜单)?

时间:2012-03-01 19:33:11

标签: android listview expandablelistview listadapter expandablelistadapter

我正在开发一个Android应用程序。到目前为止它的作用。我在一个地方使用CursorTreeAdapter。我需要在CursorTreeAdapter中为组添加按钮。我可以轻松添加textview但是当我添加一个按钮时,我的列表不起作用。它没有扩大。如何正确添加按钮?如果可以,您能给我一些代码示例吗?我试图让它修改这两个函数,但当我向我的xml gorup文件添加按钮时,我的列表不会扩展。

@Override
    protected void bindGroupView(View view, Context context, Cursor cursor,
            boolean isExpanded) {
        TextView text_line1 = (TextView) view
                .findViewById(R.id.work_list_group_view);
        text_line1.setText("title1");

        TextView text_line2 = (TextView) view
                .findViewById(R.id.work_list_group_view2);
        text_line2.setText("title2");

        ImageButton button = (ImageButton)    view.findViewById(R.id.context_menu_button);


    }

    @Override
    public View newGroupView(Context context, Cursor cursor,
            boolean isExpanded, ViewGroup parent) {
        return getLayoutInflater().inflate(
                R.layout.work_list_expandable_group, parent, false);
    }

2 个答案:

答案 0 :(得分:0)

protected void bindGroupView(View view, Context context, Cursor cursor,
                             boolean isExpanded) { 
    TextView text_line1 = (TextView) view
             .findViewById(R.id.work_list_group_view);
    text_line1.setText("title1");

    text_line1.setOnClickListener(new View.OnClickListener() { 
        // Open your dialog here 
    });

    TextView text_line2 = (TextView) view
             .findViewById(R.id.work_list_group_view2);
    text_line2.setText("title2");

    ImageButton button = (ImageButton) view
                .findViewById(R.id.context_menu_button);
}

答案 1 :(得分:0)

@Override
protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
    TextView text_line1 = (TextView) view.findViewById(R.id.work_list_group_view);
    text_line1.setText("title1");

    TextView text_line2 = (TextView) view.findViewById(R.id.work_list_group_view2);
    text_line2.setText("title2");

    ImageButton button = (ImageButton) view.findViewById(R.id.context_menu_button);
    // Magic comes here, you should add:
    button.setFocusable(false); // ListItem is not clickable if it has focusable child's.
    button.setOnClickListener(new OnClickListener(){ ... });

}

@Override
public View newGroupView(Context context, Cursor cursor,
        boolean isExpanded, ViewGroup parent) {
    return getLayoutInflater().inflate(R.layout.work_list_expandable_group, parent, false);
}