ExpandableListView 在子项内带有按钮

发布于 2024-12-12 16:18:14 字数 1312 浏览 4 评论 0原文

我正在编写一个带有 SimpleCursorTreeAdapter 的 ExpandableListActivity 来查询数据库。我在非常小的孩子中添加了一个按钮,现在我很难从单击其按钮的列表中获取有关该项目的信息。我认为我的问题是我不太了解如何处理适配器内的数据。大多数示例都是关于 listadapter 的,例如 这个。 我的适配器获得了 getChildView 实现,但我无法实现的是从原始光标中检索单击组的 _id 。因为我不知道如何操作,所以我尝试在以下代码中从组文本视图中获取名称,但在我单击的每个子项中,我都会获取列表中第一项的名称。

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
        v.inflate(getApplicationContext(),R.layout.productsexpchilditem, null);

        Button button = (Button)v.findViewById(R.id.button1);
        String name=((TextView)parent.findViewById(R.id.txt_exp_products_name_item)).getText().toString();
        button.setTag(name); 

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ProductsActivityExp.this, "TOAST "+v.getTag(), Toast.LENGTH_SHORT).show();
            }
        });      
        return v;
    }

BTY 我没有实现 getGroupView 方法,因为当我这样做时,我丢失了组中所有显示的文本(但不在子组中)。我真的认为使用cursorTreeAdapter 的具体实现与listadapter 不同。

I'm writing an ExpandableListActivity with a SimpleCursorTreeAdapter that queries a DB. I added a button inside very child, and now i'm having bad time getting info on the item from the list that its button is clicked. I think that my problem is that i don't well understand how to handle data within the adapter. Most of the examples are about listadapter like this one.
My adapter got getChildView implementation and what i cannot achieve is to retrieve the _id of the clicked group from the original cursor. Because i didn't know how, i tried in the following code to get the name from the group textview but in every child that i click i get the name of the first item in the list.

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
        v.inflate(getApplicationContext(),R.layout.productsexpchilditem, null);

        Button button = (Button)v.findViewById(R.id.button1);
        String name=((TextView)parent.findViewById(R.id.txt_exp_products_name_item)).getText().toString();
        button.setTag(name); 

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ProductsActivityExp.this, "TOAST "+v.getTag(), Toast.LENGTH_SHORT).show();
            }
        });      
        return v;
    }

BTY I didn't implemented the getGroupView method, cause when I did it I lost all the displayed text in the groups (but not in the child). I really think the work with cursorTreeAdapter got specific implementation different then listadapter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2024-12-19 16:18:14

我没有使用 ExpandableListView,但通过父级获取名称值似乎很奇怪。所有按钮的名称都相同,这并不奇怪。
你能详细解释一下你想做什么吗?

I didn't used ExpandableListView, but it seems strange that you get name value via parent. Its not surprising that you get same name for all buttons.
Can you explain in details, what you want to do?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文