简体   繁体   English

绑定视图持有者不在适配器中运行

[英]on bind view holder does not run in adapter

I have a strange problem. 我有一个奇怪的问题。 I want to show some products in a RecyclerView . 我想在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来设置适配器

recyclerView.setAdapter(searchItemAdapter); 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, 而不是使用viewgroup.getContext() ,在适配器中传递活动上下文,如下所示,

List<Product> products;
Context context;
public SearchItemAdapter(Context context){
    this.context = context;
    products=new ArrayList<>();
}

and use context, 并使用上下文,

hope your problem solve. 希望你的问题解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不一致检测到无效的 View Holder 适配器错误 - Inconsistency Detected Invalid View Holder Adapter Error 如何使用视图持有者图案制作自定义适配器? - How to make custom adapter with view holder pattern? 元素的自定义适配器视图持有者位置 - Custom adapter view holder positions of elements 这会在视图持有者中泄漏构造函数吗? - Does this leak the constructor in the view holder? RecyclerView和java.lang.IndexOutOfBoundsException视图持有者适配器positionViewHolder无效 - RecyclerView and java.lang.IndexOutOfBoundsException Invalid view holder adapter positionViewHolder 在简单的适配器中设置绑定视图? - Set bind view in simple adapter? 如何根据适配器回调列表器更新适配器中的视图持有者文本视图值? - Ho to update view holder text view value in adapter according to the adapter callback listner? 无条件布局,视图适配器膨胀:应该使用 View Holder 模式 - Unconditional layout, inflation from view adapter: Should use View Holder pattern 来自视图适配器的无条件布局膨胀:应该使用 View Holder 模式 - 使应用程序崩溃 - Unconditional layout inflation from view adapter: Should use View Holder pattern -crash the app 持有人返回错误的观点 - holder returning wrong view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM