如何使用sparsearray作为适配器的源?[英] How to use SparseArray as a source for Adapter?

本文是小编为大家收集整理的关于如何使用sparsearray作为适配器的源?的处理方法,想解了如何使用sparsearray作为适配器的源?的问题怎么解决?如何使用sparsearray作为适配器的源?问题的解决办法?如何使用sparsearray作为适配器的源?问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

I have a sparse array of values which I want to populate in a Spinner, and when the item is selected, I want to get the id (which is the key from the sparse array).

从sparsearray创建适配器的首选方法是什么?

是否可以子类像BaseAdapter或ListAdapter等现有适配器子类,以便项目将从SPARSEARRAY作为项目ID的键?

Not knowing how to achieve the above, I am thinking of creating a simple ArrayAdapter instance and giving it values from the SparseArray as a source and when the item is selected, to look up the key by the value, which I think won效率很高.

推荐答案

创建BaseAdapter的子类应该正常工作.例如.拿

public abstract class SparseArrayAdapter<E> extends BaseAdapter {

    private SparseArray<E> mData;
    public void setData(SparseArray<E> data) {
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public E getItem(int position) {
        return mData.valueAt(position);
    }

    @Override
    public long getItemId(int position) {
        return mData.keyAt(position);
    }
}

并扩展了一个获得一些实际功能.例如像

public class SparseStringsAdapter extends SparseArrayAdapter<String> {
    private final LayoutInflater mInflater;
    public SparseStringsAdapter(Context context, SparseArray<String> data) {
        mInflater = LayoutInflater.from(context);
        setData(data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView result = (TextView) convertView;
        if (result == null) {
            result = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, null);
        }
        result.setText(getItem(position));
        return result;
    }
}

本文地址:https://www.itbaoku.cn/post/1886265.html

问题描述

I have a sparse array of values which I want to populate in a Spinner, and when the item is selected, I want to get the id (which is the key from the sparse array).

What is the preferred way of creating an adapter from a SparseArray?

Is it possible to subclass an existing Adapter like BaseAdapter or ListAdapter So that the items will have a key from the SparseArray as item id?

Not knowing how to achieve the above, I am thinking of creating a simple ArrayAdapter instance and giving it values from the SparseArray as a source and when the item is selected, to look up the key by the value, which I think won't be efficient.

推荐答案

Creating a subclass of BaseAdapter should work fine. E.g. take

public abstract class SparseArrayAdapter<E> extends BaseAdapter {

    private SparseArray<E> mData;
    public void setData(SparseArray<E> data) {
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public E getItem(int position) {
        return mData.valueAt(position);
    }

    @Override
    public long getItemId(int position) {
        return mData.keyAt(position);
    }
}

and extend that one to get some actual functionality. For example like

public class SparseStringsAdapter extends SparseArrayAdapter<String> {
    private final LayoutInflater mInflater;
    public SparseStringsAdapter(Context context, SparseArray<String> data) {
        mInflater = LayoutInflater.from(context);
        setData(data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView result = (TextView) convertView;
        if (result == null) {
            result = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, null);
        }
        result.setText(getItem(position));
        return result;
    }
}
查看更多