如何从项目.java文件获取方法到卡列表适配器.java文件



我已经创建了一个具有必要的 getter 和 setter 方法的 Item.java 模型类,但我无法从我的适配器类访问这些gettersetter方法。我不知道是什么问题。

任何帮助都将是非常可观的。我在下面有与帖子相关的课程。如果还有什么需要,请发表评论。

1(型号:Item.java

package com.shinysoftware.hp.androidswiperecycler.Model;
public class Item {
    String thumbnail;
    String price;
    String name;
    String description;
    int id;
    public Item(){
    }
    public String getThumbnail ()
    {
        return thumbnail;
    }
    public void setThumbnail (String thumbnail)
    {
        this.thumbnail = thumbnail;
    }
    public String getPrice ()
    {
        return price;
    }
    public void setPrice (String price)
    {
        this.price = price;
    }
    public String getName ()
    {
        return name;
    }
    public void setName (String name)
    {
        this.name = name;
    }
    public String getDescription ()
    {
        return description;
    }
    public void setDescription (String description)
    {
        this.description = description;
    }
    public int getId ()
    {
        return id;
    }
    public void setId (int id)
    {
        this.id = id;
    }
}

2(适配器类:CardListAdapter.java

import com.shinysoftware.hp.androidswiperecycler.Model.Item;

public class CardListAdapter extends RecyclerView.Adapter<CardListAdapter.MyViewHolder> {
private Context context;
private List<Item> list;
public CardListAdapter(Context context,List<Item> list) {
    this.context = context;
    this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View itemview=LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.card_list_item,viewGroup,false);
    return new MyViewHolder(itemview);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
   final Item item=list.get(position);
   // BELOW IS MY ERROR LINE. I CAN'T GET MY METHOD (getName())
   holder.name.setText(item.getName());
}
@Override
public int getItemCount() {
    return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView name,description,price;
    public ImageView thumbnail;
    public RelativeLayout viewBackground,viewForeground;
    public MyViewHolder(View itemView) {
        super(itemView);
        name=itemView.findViewById(R.id.name);
        description=itemView.findViewById(R.id.description);
        price=itemView.findViewById(R.id.price);
        thumbnail=itemView.findViewById(R.id.thumbnail);
        viewBackground=itemView.findViewById(R.id.view_background);
        viewForeground=itemView.findViewById(R.id.view_foreground);
    }
  }
}

我认为您的适配器constructor应该如下所示。

public CardListAdapter(Context context,List<Item> list) {
this.context = context;
this.list = list;
}

适配器中的项和实际项模型是不同的对象,只是具有相同的名称,对数据模型使用更好的命名和 UNIQUE。

将适配器构造函数替换为下面的一个。 只需要从第二个参数中删除剪辑数据。

public CardListAdapter(Context context,List<Item> list) {
    this.context = context;
    this.list = list;
}

最新更新