获取列表视图标题按钮的 onlick listner 内的适配器引用

发布于 2024-11-30 06:31:26 字数 1279 浏览 3 评论 0原文

我有一个带有标题的列表视图。列表标题中有一个“前往”按钮。对于其单击,我需要调用自定义适配器中的自定义方法。如何在 side onlick lister 中获取适配器参考?

public class GroupListActivity extends ListActivity {
...
private void createGroupList() {

    final ListView listView = getListView();

    final View view = getLayoutInflater().inflate(R.layout.multi_list_groups_header, listView, false);
    listView.addHeaderView(view, null, true);
    TextView listHeader = (TextView) findViewById(R.id.groupsHeader);
    Button goButton = (Button) findViewById(R.id.goButton);

    this.gAdapter = new GroupAdapter(this, R.layout.multi_list_groups2, gStore, true);
    this.setListAdapter(this.gAdapter);
    //listView.setFocusable(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    goButton = (Button) findViewById(R.id.goButton);
    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(MY_DEBUG_TAG,"Manual Go!!");
            // I need to call the adapters' getCheckedItems() method here
        }
    });
}

定制适配器

public class GroupAdapter extends ArrayAdapter<Group> {
    ...
    public HashMap<String, String> getCheckedItems() {
        return checkedItems;
    }
}

I've a list view with a header. There is a 'Go' button in the list header. And for its on click I need to call a custom method in the Custom Adapter. How can I get the Adapter reference in side onlick lister?

public class GroupListActivity extends ListActivity {
...
private void createGroupList() {

    final ListView listView = getListView();

    final View view = getLayoutInflater().inflate(R.layout.multi_list_groups_header, listView, false);
    listView.addHeaderView(view, null, true);
    TextView listHeader = (TextView) findViewById(R.id.groupsHeader);
    Button goButton = (Button) findViewById(R.id.goButton);

    this.gAdapter = new GroupAdapter(this, R.layout.multi_list_groups2, gStore, true);
    this.setListAdapter(this.gAdapter);
    //listView.setFocusable(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    goButton = (Button) findViewById(R.id.goButton);
    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(MY_DEBUG_TAG,"Manual Go!!");
            // I need to call the adapters' getCheckedItems() method here
        }
    });
}

Custom Adapter

public class GroupAdapter extends ArrayAdapter<Group> {
    ...
    public HashMap<String, String> getCheckedItems() {
        return checkedItems;
    }
}

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

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

发布评论

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

评论(1

红玫瑰 2024-12-07 06:31:26

也许您想创建一个自定义 Button 来保存 GroupAdapter 的引用。这样,您就可以将传入的 View 对象强制转换为自定义 Button,然后获取您的 GroupAdapter

    @Override
    public void onClick(View v) {
        Log.e(MY_DEBUG_TAG,"Manual Go!!");
        Map<String, String> checkedItems = ((MyButton)v).getGroupAdapter().getCheckedItems();
    }

Perhaps you want to create a custom Button that holds on to a reference of your GroupAdapter. That way, you can cast the passed in View object to your custom Button, and then get your GroupAdapter.

    @Override
    public void onClick(View v) {
        Log.e(MY_DEBUG_TAG,"Manual Go!!");
        Map<String, String> checkedItems = ((MyButton)v).getGroupAdapter().getCheckedItems();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文