我正在制作一个示例领域应用程序以获得乐趣。领域版本:0.88.2
我遇到的问题是每次我尝试修改我的元素时,我的排序设置都会自动消失。
这是我的步骤: 我有一个歌曲列表。
代码如下:
MainActivity:
public class MainActivity extends AppCompatActivity implements OnClickInterface {
@Bind(R.id.tool_bar)
Toolbar toolBar;
@Bind(R.id.list)
ListView list;
private Realm realm;
boolean isChecked = false;
RealmListviewAdapter adapter;
boolean sort = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolBar);
realm = Realm.getDefaultInstance();
adapter = new RealmListviewAdapter(this, null, true);
adapter.setListener(this);
list.setAdapter(adapter);
if (isChecked) {
updateAllSongs();
} else {
realm.where(Song.class).findFirstAsync().asObservable()
.filter(new Func1<RealmObject, Boolean>() {
@Override
public Boolean call(RealmObject realmObject) {
return realmObject.isLoaded();
}
})
.first()
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<RealmObject>() {
@Override
public void call(RealmObject realmObject) {
if (!realmObject.isValid()) {
addUsers();
} else {
updateAllSongs();
}
}
});
isChecked = true;
}
}
@OnClick(R.id.tool_bar)
public void onClickToolbar() {
if (sort) {
adapter.getRealmResults().sort("_id");
} else {
adapter.getRealmResults().sort("_id", Sort.DESCENDING);
}
sort = !sort;
adapter.notifyDataSetChanged();
}
private void addUsers() {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
String[] titles = getResources().getStringArray(R.array.song_titles);
for (int i = 0; i < titles.length; i++) {
Song song = new Song();
song.set_id(i);
song.set_trackNumber(i + 1);
song.set_title(titles[i]);
song.set_favorite(false);
song.set_frequency(0);
song.set_lyrics("");
song.set_downloaded(false);
realm.copyToRealm(song);
}
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
updateAllSongs();
}
});
}
private void updateAllSongs() {
realm.where(Song.class).findAllSortedAsync("_id").asObservable()
.filter(new Func1<RealmResults<Song>, Boolean>() {
@Override
public Boolean call(RealmResults<Song> songs) {
return songs.isLoaded();
}
})
.first()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<RealmResults<Song>>() {
@Override
public void call(RealmResults<Song> songs) {
adapter.updateRealmResults(songs);
}
});
}
@Override
public void onClickedItem(int position) {
System.out.println("Pressed position " + position);
Song song = adapter.getRealmResults().get(position);
realm.beginTransaction();
Log.d("frequency", String.valueOf(song.get_frequency()));
song.set_frequency(song.get_frequency() + 1);
realm.commitTransaction();
Log.d("frequency", String.valueOf(song.get_frequency()));
//Log.d("frequency", String.valueOf(_data.get(position).get_frequency()));
}
}
我的适配器:
public class RealmListviewAdapter extends RealmBaseAdapter<Song> implements ListAdapter{
private OnClickInterface _listener;
public RealmListviewAdapter(Context context, RealmResults<Song> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SongViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.eachsong, parent, false);
viewHolder = new SongViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (SongViewHolder) convertView.getTag();
}
Song song = realmResults.get(position);
viewHolder.track.setText(String.valueOf(song.get_trackNumber()));
viewHolder.title.setText(song.get_title());
viewHolder.position = position;
return convertView;
}
public void updateRealmResults(RealmResults<Song> queryResults) {
super.updateRealmResults(queryResults);
Log.d("Adapter", "UpdateRealmResults is called");
}
public RealmResults<Song> getRealmResults() {
return realmResults;
}
public void setListener(OnClickInterface listener) {
_listener = listener;
}
class SongViewHolder{
@Bind(R.id.tracknumber)
TextView track;
@Bind(R.id.title)
TextView title;
@Bind(R.id.freq)
TextView freq;
@Bind(R.id.fav_star)
ImageView fav;
int position;
public SongViewHolder(View itemView) {
ButterKnife.bind(this, itemView);
}
@OnClick(R.id.item_row)
public void onClickItem() {
_listener.onClickedItem(position);
}
}
}
请帮我解决这个问题。非常感谢你。