我是图书馆Realm和RxJava的新先驱,我有一个问题:
如何设置新数据库并在其上设置自动更新?而不是我的主要代码,Realm团队建议使用Disposable,我试图这样做,但我有一个例外:Unable to start activity ComponentInfo{ java.lang.IllegalArgumentException: Invalid query: field 'getList' not found in class 'Board'.
但我有这个字段
@SerializedName("list")
@Expose
private RealmList<List> list = new RealmList<>();
public RealmList<List> getList() {
return list;
}
尝试:
disposable = realm.where(Board.class).isNotNull("getList").findAllAsync().asFlowable()
.filter(boards -> board.isLoaded())
.flatMap(boards -> service.getFlowableBoard(currentBoard))
.doOnNext(list -> {
if (list != null) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(list);
realm.commitTransaction();
realm.close();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(boards -> {
recyclerView.setAdapter(new BoardAdapter(MainActivity.this, board.getList(), board.getBoard()));
});
} else {
realm.copyToRealm((realm.where(Board.class).findAll()));
}
我的基本硬编码在这里:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycle_view);
Intent intent = getIntent();
String currentBoard = intent.getStringExtra(GET_B);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
OrderedRealmCollection<List> rr = null;
Board board = new Board();
BoardAdapter adapter = new BoardAdapter(this, rr, board.getBoard());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
Log.e("MY_LOGS", currentBoard);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
//We need to use Realm.init, because without it, we will catching exception
Realm.init(MainActivity.this);
//We need to use,without - we will get Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
realm = Realm.getDefaultInstance();
Service service = retrofit.create(service.class);
Observable<Board> observable = Observable.create(e -> realm.copyFromRealm(realm.where(Board.class).findAll()));
if (isNetworkAvialable()) {
service.getRxBoard(currentBoard)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.doOnNext(database -> {
if (database != null) {
Realm realm = Realm.getDefaultInstance();
//problem
realm.executeTransaction(trRealm -> {
trRealm.copyToRealmOrUpdate(database);
});
} else {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(database);
realm.commitTransaction();
realm.close();
}
})
.mergeWith(observable)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(board1 -> {
//The second parameter - RealmList
recyclerView.setAdapter(new BoardAdapter(MainActivity.this, board.getList(), board.getBoard()));
}, Throwable::printStackTrace);
} else {
realm.copyToRealm((realm.where(Board.class).findAll()));
}
}
在此之后我有一个例外:Only use this adapter with managed RealmCollection, for un-managed lists you can just use the BaseRecyclerViewAdapter
我的适配器在这里:
public class BoardAdapter extends RealmRecyclerViewAdapter<List, BoardAdapter.ViewHolder> {
private static String TAG = "MY LOGS";
String board;
private Context context;
public BoardAdapter(Context context, OrderedRealmCollection<List> list, String board) {
super(list, true);//Problem is here
this.context = context;
this.board = board;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView subject;
TextView comment;
TextView views;
ImageView image;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
subject = itemView.findViewById(R.id.subject);
comment = itemView.findViewById(R.id.comment);
views = itemView.findViewById(R.id.views);
image = itemView.findViewById(R.id.imageView);
cardView = itemView.findViewById(R.id.opPost);
}
}
@Override
public BoardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.board_activity, parent, false);
Log.d(TAG, "onCreateViewHolder: " + view + viewType);
return new BoardAdapter.ViewHolder(view);
}
private List<Board> boards;
@Override
public void onBindViewHolder(BoardAdapter.ViewHolder holder, final int position) {
final List list = getData().get(position);//The getData() was previously missing
holder.subject.setText(Html.fromHtml(list.getSubject()));
holder.comment.setText(Html.fromHtml(list.getComment()));
holder.views.setText(Html.fromHtml(list.getDate()));
holder.image.setVisibility(View.GONE);
int files = list.getFiles().size();
if (files > 0) {
for (File file : list.getFiles()) {
if (file.getPath() != null) {
holder.image.setVisibility(View.VISIBLE);
String f = file.getPath();
Picasso.with(context).load(BASE_URL + f).into(holder.image);
}
}
}
}
如果可以如何简化MainActivity中的代码?