我在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-java
和Retrofit
的初学者。
我正在使用staggeredGridView
来显示数据。所以,在我的MainActivity
课程中,我正在调用以下代码:
StaggeredGridAdapter adapter = new StaggeredGridAdapter(this);
mRecyclerView.setAdapter(adapter);
要填充StaggeredGridView holder
中的数据,我必须调用getWarehouse
方法。
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>{}
在我提出建议后,我使用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
方法的定义是什么?
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';
}
答案 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();
}
}