调用Rx-java Observable方法的正确方法是什么?

时间:2015-10-19 17:46:50

标签: java android retrofit rx-java

我在Wrapper课程中定义了以下方法,用于拨打Rest

public void getWarehouse(){

    //Generation of  RestAdapter
    RestAdapter adapter = new  RestAdapter.Builder ()
            .setEndpoint(URL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    //Making request to API
    adapter.create(WarehouseAPI.class).getWarehouse()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Warehouse>>() {
                @Override
                public void onCompleted() {
                    Log.d(this.getClass().getName(), "OnCompleted()");
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(this.getClass().getName(), "Error: "  + e.toString());
                }

                @Override
                public void onNext(List<Warehouse> response) {
                }
            });
}

以上代码工作正常,我得到了预期的回复。但我需要从Adapter class调用此方法。我怎样才能做到这一点?有什么方法可以使用onNext方法吗?

我是Android rx-javaRetrofit的初学者。

修改-1

我正在使用staggeredGridView来显示数据。所以,在我的MainActivity课程中,我正在调用以下代码:

 StaggeredGridAdapter adapter = new StaggeredGridAdapter(this);
 mRecyclerView.setAdapter(adapter);

要填充StaggeredGridView holder中的数据,我必须调用getWarehouse方法。

修改-2

MainActivity&#39; onCreate()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    getWarehouse();
    mRecyclerView = (RecyclerView) findViewById(R.id.staggering_grid);
    mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

    StaggeredGridAdapter adapter = new StaggeredGridAdapter(this);
    mRecyclerView.setAdapter(adapter);
    GridItemSpaces decoration = new GridItemSpaces(10);
    mRecyclerView.addItemDecoration(decoration);

}

StaggeredGridAdapter的位置:

public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView>{}

修改-3

在我提出建议后,我使用getWarehouse();方法拨打MainActivity.onCreate并将getWarehouse()的定义复制到MainActivity.class

也是我的MainActivity.getWarehouse.onNext方法:

@Override
public void onNext(List<Warehouse> response) {
  mAdapter.addItems(response);
}

mAdapter private StaggeredGridAdapter mAdapter;的位置MainActivity.onCreate稍后在addItems方法中初始化。

StaggeredGridAdapter类中StaggeredGridAdapter方法的定义是什么?

修改-4

public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView> { private Context context; private List<Warehouse> warehouses = new ArrayList<Warehouse>(); int size; public StaggeredGridAdapter(Context context) { this.context = context; } public void addItems(List<Warehouse> response){ size = response.size(); warehouses = response; } @Override public StaggeredGridView onCreateViewHolder(ViewGroup parent, int viewType) { View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false); StaggeredGridView staggeredGridView = new StaggeredGridView(layoutView); return staggeredGridView; } @Override public void onBindViewHolder(StaggeredGridView holder, int position) { holder.textView.setText(warehouses.get(position).getFace()); } @Override public int getItemCount() { return size; } class StaggeredGridView extends RecyclerView.ViewHolder { TextView textView; public StaggeredGridView(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.img_name); } }

的完整定义
.privacycheck1 {
    position: relative;
    background-color: #E60000;
    width: 24px;
    height: 24px;
    border-radius: 50px;
    border: 5px #E60000;   
}

.privacycheck1::before {
    content: 'i';
    position: relative;
    display: block;
    height: 20px;
    width: 200px;
    left: 30px;
}

.privacycheck1:hover::before {
    content: 'This information is private';
}

2 个答案:

答案 0 :(得分:0)

getWarehouse不是在getWarehouse方法中订阅,而是返回observable并在适配器中执行subscribe部分。这样,您可以在适配器中存在onNext回调,而不是在调用方法中。理想情况下,您应该只在getWarehouse方法中实例化一次restadapter,而不是每次都实例化。

public Observable<Warehouse> getWarehouse(){

adapter.getWarehouse()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());

}

来自适配器的

执行此操作: someClass.getWarehouse().subscribe(new Subscriber()....)

答案 1 :(得分:0)

我会将getWarehouse()留在Activity。在同一个Activity中,您可以像使用mRecyclerView一样保留对StaggeredGridAdapter的引用。在适配器中创建public void addItems(List<Warehouse> response),当调用onNext时,您只需执行mAdapter.addItems(response)

E.g。

 public void addItems(List<Warehouse> items) {
     int oldSize = mDataSet.size();
     synchronized(mDataSet) {
         mDataSet.addAll(items);
     }
     notifyItemRangeInserted(oldSize - 1, mDataSet.size() - 1);
 }

我假设mDataSet是您在适配器(您的数据集)中使用的Collection的名称。

我也会避免实例化

   RestAdapter adapter = new  RestAdapter.Builder ()
            .setEndpoint(URL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

每次拨打getWarehouse();时。 subscribe返回Subscription个对象。将其保留为成员变量,并在ondDestroy中调用取消订阅

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mSubscription != null) {
        mSubscription.unsubscribe();
    }
}