2016-08-20 4 views
1

Я пытался хранить массив строк в Realm базе программно, как указано ниже:Realm Массив строк в Android

Модель Класс:

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

Есть ли альтернативный лучший способ для этого?

ответ

0

Ну да, конечно, есть

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

EDIT: Проверьте этот сексуальный кок.

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

Что такое "listOfStationNames" здесь? Я не получил –

+0

содержимое 'name1',' name2', ... 'name8000' в списке – EpicPandaForce

+0

Быстрый ответ ... thankyou..I придется проверить это ... Я комментирую здесь, как только я закончил –

Смежные вопросы