我有一个奇怪的问题。我想在RecyclerView
中展示一些产品。我将列表传递给适配器以显示。在适配器的构造函数中,设置了列表,但未调用其他方法。在调试时,列表已设置,但是没有其他反应,也不会显示任何内容
这是我的适配器代码:
public class SearchItemAdapter extends RecyclerView.Adapter<SearchItemAdapter.SearchViewHolder> {
List<Product> products;
public SearchItemAdapter(){
products=new ArrayList<>();
}
@NonNull
@Override
public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_item,viewGroup,false);
return new SearchViewHolder(view);
}
public void onbind(List<Product> products){
this.products=products;
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull SearchViewHolder searchViewHolder, int i) {
Product product= products.get(i);
Picasso.get().load(product.getPic()).into(searchViewHolder.image);
searchViewHolder.txtTitle.setText(product.getTitle());
searchViewHolder.txtDesc.setText(product.getTitle());
}
@Override
public int getItemCount() {
return products.size();
}
public class SearchViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView txtTitle,txtDesc;
public SearchViewHolder(@NonNull View itemView) {
super(itemView);
txtTitle=itemView.findViewById(R.id.txt_searchItem_title);
txtDesc=itemView.findViewById(R.id.txt_searchItem_desc);
image=itemView.findViewById(R.id.img_searchItem_image);
}
}
}
和活动代码:
public class CompareSearchActivity extends AppCompatActivity {
CompareViewModel viewModel=new CompareViewModel();
CompositeDisposable compositeDisposable=new CompositeDisposable();
EditText edtSearch;
ProgressBar progressBar;
ImageView imgClose;
RecyclerView recyclerView;
SearchItemAdapter searchItemAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compare_search);
setupViews();
}
private void setupViews() {
recyclerView=findViewById(R.id.rv_compareSearch_searchedProduct);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchItemAdapter=new SearchItemAdapter();
recyclerView.setAdapter(searchItemAdapter);
imgClose=findViewById(R.id.img_compareSearch_close);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
edtSearch=findViewById(R.id.edt_compareSearch_serach);
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
viewModel.getSearchedProduct(String.valueOf(s))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<Product>>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(List<Product> products) {
progressBar.setVisibility(View.GONE);
searchItemAdapter.onbind(products);
}
@Override
public void onError(Throwable e) {
Log.i("LOG", "onError: "+e.toString());
}
});
}
@Override
public void afterTextChanged(Editable s) {
}
});
progressBar=findViewById(R.id.progress_compareSearch);
}
}
答案 0 :(得分:0)
您忘记了RecyclerView来设置适配器
recyclerView.setAdapter(searchItemAdapter);
并更改代码,如下所示
public void onbind(List<Product> products){
this.products.clear();
this.products.addAll(products);
notifyDataSetChanged();
}
按如下所示设置适配器
private void setupViews() {
recyclerView=findViewById(R.id.rv_compareSearch_searchedProduct);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchItemAdapter=new SearchItemAdapter();
// Here is the adapter
recyclerView.setAdapter(searchItemAdapter);
imgClose=findViewById(R.id.img_compareSearch_close);
imgClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
edtSearch=findViewById(R.id.edt_compareSearch_serach);
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
viewModel.getSearchedProduct(String.valueOf(s))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<Product>>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(List<Product> products) {
progressBar.setVisibility(View.GONE);
searchItemAdapter.onbind(products);
}
@Override
public void onError(Throwable e) {
Log.i("LOG", "onError: "+e.toString());
}
});
}
@Override
public void afterTextChanged(Editable s) {
}
});
progressBar=findViewById(R.id.progress_compareSearch);
}
}
答案 1 :(得分:0)
我不清楚,但是,我有一个建议给你
而不是使用viewgroup.getContext()
,而是按如下所述在适配器中传递活动上下文,
List<Product> products;
Context context;
public SearchItemAdapter(Context context){
this.context = context;
products=new ArrayList<>();
}
并使用上下文
希望您的问题得到解决。