android - 在 ListView 标题按钮的 onclick 监听器中获取适配器引用

标签 android android-listview adapter

我有一个带有标题的 ListView 。列表标题中有一个“开始”按钮。为了点击它,我需要在自定义适配器中调用自定义方法。如何在侧面 onlick 列表器中获取适配器引用?

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;
    }
}

最佳答案

也许您想创建一个自定义的 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();
    }

关于android - 在 ListView 标题按钮的 onclick 监听器中获取适配器引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7117244/

相关文章:

android - 如何删除 Android GoogleMap 上的特定标记

android - 如何限制 Android 设备网络速度以进行测试

android - 即使在 onHandleIntent 完成后 IntentService 仍在后台运行

android - 在 Android 中的 AsyncTask 之后更新 CustomArrayAdapter

android - 检查 ListView 中的所有适配器元素

c# - 覆盖 'public' 继承成员时无法更改访问修饰符 '

android - 从 DB 文件创建 Android Room 实体类

android - 抽屉导航在滚动时更改背景

java - ListView 适配器不工作,未调用 getView。

java - 适配器与工厂模式