Realm Array of Strings in Android

realm android-database

846 просмотра

1 ответ

I have been trying to store an array of strings in Realm database programmatically as given below:

Model Class:

  public class Station extends RealmObject {
  private String name;

  // ... Generated getters and setters ...
  }

Saving Data:

  realm.executeTransactionAsync(new Realm.Transaction() {
  @Override
  public void execute(Realm realm) {
    Station station1 =     realm.createObject(Station.class)
    station1.setName(name1);
   Station station2 = realm.createObject(Station.class)
    station2.setName(name2);
  //goes on till station8000
   }
   }, new Realm.Transaction.OnSuccess() {
  @Override
  public void onSuccess() {
    // ...
 });

Is there an alternate best way for this?

Автор: V1 Kr Источник Размещён: 08.11.2019 11:16

Ответы (1)


0 плюса

Решение

Why yes of course there is

public class Station extends RealmObject {
    private String name;

    // ... Generated getters and setters ...
}

and

// field variable
RealmResults<Station> stations;
// field variable
RealmChangeListener<RealmResults<Station>> changeListener = new RealmChangeListener<RealmResults<Station>>() {
    @Override
    public void onChange(RealmResults<Station> results) {
        // handle onSuccess()
    }
}

and

stations = realm.where(Station.class).findAll();
stations.addChangeListener(changeListener);

realm.executeTransactionAsync(new Realm.Transaction() {
   @Override
   public void execute(Realm realm) {
      Station station = new Station();
      for(String stationName : listOfStationNames) {
          station.setName(stationName);
          realm.insert(station);
      }
   }
});

EDIT: Check out this sexy spinner.

public class DropdownSpinnerAdapter
        extends BaseAdapter
        implements SpinnerAdapter {
    private static final String TAG = "DropdownSpinnerAdapter";

    private boolean isItemSelected;

    RealmResults<Station> content;

    public ResultDropdownSpinnerAdapter(RealmResults<Station> objects) {
        this.content = objects;
    }

    @Override
    public int getCount() {
        if(content == null || !content.isValid()) {
            return 1;
        }
        return content.size() + 1;
    }

    @Override
    public String getItem(int position) {
        if(position <= 0) {
            return "";
        }
        return content.get(position - 1);
    }

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

    public int findPosition(Station selectedItem) {
        for(int i = 0, s = content.size(); i < s; i++) {
            Station item = content.get(i);
            if(item.equals(selectedItem)) {
                return i + 1;
            }
        }
        return 0;
    }


    static class ViewHolder {
        TextView textView;
        ImageView imageView;

        public ViewHolder(View convertView) {
            textView = ButterKnife.findById(convertView, R.id.dropdown_textview);
            imageView = ButterKnife.findById(convertView, R.id.dropdown_arrow);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView != null) {
            if(!(convertView instanceof DropdownHeaderView)) {
                convertView = null;
            }
        }
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate((isItemSelected) ? R.layout.dropdown_selected : R.layout.dropdown,
                            parent,
                            false);
            ViewHolder viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.textView.setText(getItem(position).getName());
        return convertView;
    }

    public void setItemSelected(boolean selected) {
        this.isItemSelected = selected;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(convertView != null) {
            if(!(convertView instanceof DropdownView)) {
                convertView = null;
            }
        }
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dropdown_noarrow, parent, false);
            ViewHolder viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.textView.setText(getItem(position).getName());
        return convertView;
    }

    public void updateContent(RealmResults<Station> content) {
        this.content = content;
        notifyDataSetChanged();
    }
}
Автор: EpicPandaForce Размещён: 20.08.2016 01:02
Вопросы из категории :
32x32