【问题标题】:get the Adapter reference inside onlick listner for the listview header button获取列表视图标题按钮的 onclick 侦听器内的适配器引用
【发布时间】:2011-08-19 05:17:00
【问题描述】:

我有一个带有标题的列表视图。列表标题中有一个“开始”按钮。对于它的点击,我需要在自定义适配器中调用一个自定义方法。如何在侧 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;
    }
}

【问题讨论】:

    标签: android android-listview adapter


    【解决方案1】:

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

    【讨论】:

    • 如何在自定义按钮中引用适配器?
    • 如果你定义了构造函数来接受Adapter,那么你可以保留它的引用。
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多