我想通过订阅从数据库获取数据的Observable
填充导航抽屉,完成后通知订阅者(使用RxJava
)。我已经做了类似的事情,Subscriber
是一个扩展Fragment
的类,但不同之处在于导航抽屉不是一个单独的片段,而是在MainActivity中定义的。
如何订阅Navigation Drawer
到Observable
?
MainActivity.java
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
public void onStart() { mLoadAndStoreDataObservable = Observable.create(
super.onStart(); new Observable.OnSubscribe<String>() {
// fragment creation code was here
// populates a String[] myStringArray for Navigation Drawer permitsSQLiteManager.addLogLine(mActivity, logString);
if (!skipRestfulServiceCall) {
getDataFromRestfulService(); }
} catch (Exception e) {
else { // get data from SQLite Log.e("loadAndStoreData ", "Exception: " + e.getMessage());
getDataFromSQLite(); mProgressDialog.dismiss();
} }
}
mTitle = mDrawerTitle = getTitle(); }
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); .subscribeOn(Schedulers.io())
mDrawerList = (ListView) findViewById(R.id.left_drawer); .observeOn(AndroidSchedulers.mainThread())
.subscribe(mDrawerLayout); // parameter was a Fragment
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, myStringArray));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void getDataFromRestfulService() {
<get data>
// implement RxJava-Observable/Subscriber
mLoadAndStoreDataObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
Utilities.loadAndStoreData(mActivity);
subscriber.onNext("Utilities.loadAndStoreData Done");
//subscriber.onCompleted();
Log.e("MainActivity.onCreate()", "subscriber.onNext(\"Utilities.loadAndStoreData Done\")");
String logString = "MainActivity.onCreate() - subscriber.onNext(Utilities.loadAndStoreData Done)";
Log.e(TAG, logString);
PermitsSQLiteManager permitsSQLiteManager = PermitsSQLiteManager.getInstance();
permitsSQLiteManager.addLogLine(mActivity, logString);
mProgressDialog.dismiss();
}
catch (Exception e) {
Log.e("loadAndStoreData ", "Exception: " + e.getMessage());
subscriber.onError(e);
mProgressDialog.dismiss();
}
}
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mDrawerLayout); // compile error // suscriber was a Fragment, but no longer
}
答案 0 :(得分:2)
如果mDrawerLayout是实现Observer
的类的实例,则可以将其强制转换为Observer
NavigationDrawer mDrawerLayout = ...
yourObservable.subscribe((Observer)mDrawerLayout);
如果它没有实现Observer
界面,那么您可以创建一个新的Observer
来操纵您的导航抽屉。 (您可能需要声明mDrawerLayout
为最终版)
final NavigationDrawer mDrawerLayout = ...
yourObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
// on completed
}
@Override
public void onError(Throwable throwable) {
// on error
}
@Override
public void onNext(String people) {
mDrawerLayout.doSomething();
}
});
答案 1 :(得分:2)
假设您想要从News.java类接收更新。
所以你要从你的新闻类实现Observer接口
News implements Observable{
public void publishNews(){
String newNews = "newNews";
//Now notify Users that new news is available by calling the following
// two methods
setChanged();
notifyObservers(); // This will call the subscribers and notify them
// that there is a change.
}
}
现在,您希望用户接收更新。
public class User implements Observer(){
}
将此添加到要创建User类变量的任何类。此代码确保user1,user2对象被&#34;订阅&#34;新闻类
News news = new News();
User user1 = new User();
User user2 = new User();
news.add(user1); // You have to instantiate News object to user the add method
news.add(user2); // from Observable interface.
在更新任何内容时添加逻辑。 update()方法由Observer提供 接口
@Overrride
public void update(Observable, Object){
// This gets called after notifyObservers() in the Observable class
// Whatever you want to do after Observable calls notifyObservers, goes here
}