【问题标题】:Realm Array of Strings in AndroidAndroid中的领域字符串数组
【发布时间】:2016-08-22 00:56:05
【问题描述】:

我一直在尝试以编程方式将字符串数组存储在 Realm 数据库中,如下所示:

模型类:

  public class Station extends RealmObject {
  private String name;

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

保存数据:

  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() {
    // ...
 });

还有其他最好的方法吗?

【问题讨论】:

    标签: realm android-database


    【解决方案1】:

    为什么当然有

    public class Station extends RealmObject {
        private String name;
    
        // ... Generated getters and setters ...
    }
    

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

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

    编辑:看看这个性感的旋转器。

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

    【讨论】:

    • 这里的“listOfStationNames”是什么?没看懂
    • 列表中name1name2、...name8000的内容
    • 快速响应...谢谢..我将不得不检查一下...一旦完成,我会在这里发表评论
    • 但是你没有提到“realm.copyToRealm”或“realm.createObject”。我不应该使用其中任何一个吗?我也没有在他们的文档中看到“realm.insert”..
    • 也许我读了一篇关于 toString 的旧教程!天呐!我目前正在这样做:ArrayAdapter(this, android.R.layout.simple_list_item_1, RealmResults a)..now lemme check with your advice of toString..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多