1

На одной из активностей есть RecyclerView. При создании адаптера возникают некоторые проблемы. После запуска активности, элементов RecyclerView не видно, хотя по отладке все вроде должно работать.

Код Адаптера:

class ResultViewAdapter extends RecyclerView.Adapter<ResultViewAdapter.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<Recept> recepts;

ResultViewAdapter(Context context, List<Recept> recepts) {
    this.recepts = recepts;
    this.context = context;
    this.inflater = LayoutInflater.from(context);
}
@Override
public ResultViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.result_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ResultViewAdapter.ViewHolder holder, int position) {
    Recept recept = recepts.get(position);
    //holder.imageView.setImageResource(phone.getImage());
    holder.nameView.setText(recept.getName());
    holder.discrView.setText(recept.getIngridients());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    //final ImageView imageView;
    final TextView nameView, discrView;
    final CardView cv;
    ViewHolder(View view){
        super(view);
       // imageView = (ImageView)view.findViewById(R.id.image);
        nameView = (TextView) view.findViewById(R.id.name);
        discrView = (TextView) view.findViewById(R.id.discr);
        cv = (CardView) view.findViewById(R.id.cv);
    }
}
}

Код класса Recept:

public class Recept {
String name,discription;
String ingridients;
public Recept(String name, String discription, String ingridients){
    this.name = name;
    this.discription = discription;
    this.ingridients = ingridients;
}
public Recept(String name,String ingridients){
    this.name = name;
    this.ingridients = ingridients;
}

public String getIngridients() {

    return ingridients;
}

public void setIngridients(String ingridients) {
    this.ingridients = ingridients;
}

public String getDiscription() {
    return discription;
}

public void setDiscription(String discription) {
    this.discription = discription;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

Фрагмент кода активности:

    protected void onResume() {
    super.onResume();

    List<Recept> list_test = new ArrayList<Recept>();
    list_test.add(new Recept("test","Disc"));
    ResultViewAdapter adapter1 = new ResultViewAdapter(ResultActivity.this, list_test);
    rv.setAdapter(adapter1);
}

Как это исправить?

1 ответ 1

1

Добавьте LayoutManager, без него RecyclerView не отображается.

LinearLayoutManager llm = new LinearLayoutManager(this);
или
GridLayoutManager glm = new GridLayoutManager(this,4); 
и
recyclerView.setLayoutManager(llm); или recyclerView.setLayoutManager(glm);
1
  • Вау. Спасибо, работает.
    – Felay
    3 мая 2018 в 15:49

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.