I have a strange problem. I want to show some products in a RecyclerView
. I pass a list to the adapter to show. In the constructor of the adapter, the list is set, but no other method is called. While I'm debugging, the list is set, but nothing else happens and nothing will be displayed
This is my code for the adapter:
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);
}
}
}
and activity codes:
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);
}
}
You forget RecyclerView to set adapter
recyclerView.setAdapter(searchItemAdapter);
And change the code as well as like below
public void onbind(List<Product> products){
this.products.clear();
this.products.addAll(products);
notifyDataSetChanged();
}
Set adapter like below
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);
}
}
i am not clear about that but, i have one suggestion for you
instead of using viewgroup.getContext()
, pass activity context in adapter as below,
List<Product> products;
Context context;
public SearchItemAdapter(Context context){
this.context = context;
products=new ArrayList<>();
}
and use context,
hope your problem solve.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.