适配器无法应用于recycleview搜索

时间:2018-03-28 15:59:19

标签: android android-recyclerview

我正在尝试将SeachAdapter调用到我的Activity但是我无法获取列表值,因为它无法应用显示

这是我的活动代码,错误相同

adapter = new SearchAdapter(this.mOwnerDao.getOwner());
    recyclerView.setAdapter(adapter);

}

private void startSearch(String text) {
    adapter = new SearchAdapter(this.mOwnerDao.getOwnerByName(text));
    recyclerView.setAdapter(adapter);
}

我的SearchAdapter

public class SearchAdapter  extends  RecyclerView.Adapter<SearchViewHolder>{
private Context context;
private List<Owner> owners;

public SearchAdapter(Context context, List<Owner> owners)
{
    this.context = context;
    this.owners = owners;
}


@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View itemView = inflater.inflate(R.layout.layout_item,parent,false);
    return new SearchViewHolder(itemView);
}

@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
    holder.name.setText(owners.get(position).getName());
    holder.phoneNumber.setText(owners.get(position).getPhoneNumber());
    holder.address.setText(owners.get(position).getAddress());

}

@Override
public int getItemCount() {
    return owners.size();
}
}

3 个答案:

答案 0 :(得分:0)

试试这个: -

private void startSearch(String text) {
    adapter = new SearchAdapter(this, this.mOwnerDao.getOwnerByName(text));
    recyclerView.setAdapter(adapter);
}

答案 1 :(得分:0)

我认为您缺少布局管理器到您的recyclerview。发布更多代码。对不起,我没有评论的好评,所以发帖作为答案

答案 2 :(得分:0)

每次调用此函数时,在this函数的第一个参数处传递SearchAdapter

new SearchAdapter(this,this.mOwnerDao.getOwner());