如何从Realm的数据库表中获取所有列?

时间:2016-11-14 19:16:05

标签: android realm

我知道这个问题很愚蠢,但我需要使用Realm创建Spinner并为此获取一列。

我想要的只是获取一个columt到String数组以使用ArrayAdapter。

如何获得此专栏?

或者更好的方法是扩展ArrayAdapter,我将使用循环从该列获取所有行?

请告诉我,更好的解决方案。

2 个答案:

答案 0 :(得分:1)

领域有特殊的适配器:

示例代码:

public class MyListAdapter extends RealmBaseAdapter<TimeStamp> implements ListAdapter {

    private static class ViewHolder {
        TextView timestamp;
    }

    public MyListAdapter(OrderedRealmCollection<TimeStamp> realmResults) {
        super(realmResults);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(android.R.layout.simple_list_item_1, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.timestamp = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        TimeStamp item = adapterData.get(position);
        viewHolder.timestamp.setText(item.getTimeStamp());
        return convertView;
    }
}

答案 1 :(得分:1)

如果你真的想重新发明轮子,那么首先应该避免使用ArrayAdapter。毕竟,它处理元素和视图之间的实际绑定,这意味着你学到的东西更少!

因此,如果您想学习,则应创建BaseAdapter。有了这个,我们将重新创建RealmBaseAdapter

好的,它是如何工作的,你可以扩展BaseAdapter,它需要以下方法:

public class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        // return count;
    }

    @Override
    public Object getItem(int position) {
        // return item at position;
    }

    @Override
    public long getItemId(int position) {
        // return unique identifier at position index
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // implement view holder pattern
        // bind view holder with data at position
        // return bound view
    }
}

RealmBaseAdapter还会为您提供一个RealmChangeListener,附加到您在创建时提供的RealmResults

所以你的情况会是这样的

public class YourAdapter extends BaseAdapter {
    RealmResults<YourObject> results;

    final RealmChangeListener realmChangeListener = new RealmChangeListener() {
        @Override
        public void onChange(Object element) {
            notifyDataSetChanged();
        }
    };

    public YourAdapter(RealmResults<YourObject> results) {
        this.results = results;
        results.addChangeListener(realmChangeListener);
    }

    public void updateData(RealmResults<YourObject> results) {
        if(this.results.isValid()) {
            this.results.removeChangeListener(realmChangeListener);
        }
        this.results = results;
        results.addChangeListener(realmChangeListener);
    }

    @Override
    public int getCount() {
        // return count;
        if(results == null || !results.isValid()) {
            return 0;
        }
        return results.size();
    }

    @Override
    public YourObject getItem(int position) {
        // return item at position;
        return results.get(i);
    }

    @Override
    public long getItemId(int position) {
        // return unique identifier at position index
        return position; // this is sufficient
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {        
        // implement view holder pattern
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_1, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }    

        // bind view holder with data at position
        YourObject item = results.get(position);
        viewHolder.textView.setText(item.getNeededColumn());

        // return bound view
        return convertView;
    }

    private static class ViewHolder {
        TextView textView;

        public ViewHolder(View view) {
            textView = (TextView)view.findViewById(android.R.id.text1);
        }
    }
}

然后你可以这样做:

YourAdapter yourAdapter = new YourAdapter(realm.where(YourObject.class).findAll());
listView.setAdapter(yourAdapter);

虽然我最近更喜欢RecyclerViews,但没关系