Alex Smith Alex Smith - 23 days ago 6
Android Question

How to get all column from Realm's DB table?

I know that question is stupid, but I need to create Spinner using Realm and get one column for this.
All I want is get one all columt to String array to use ArrayAdapter. How can I get this column? Or maybe the better way is extend ArrayAdapter where I will get all rows from that column using a loop? Tell me, please, the better solution.

Answer

There are special Adapters for Realm:


Example code:

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