我一直在這裏閱讀不同的答案在計算器後,試圖實現他們的解決方案,但我仍然得到錯誤RecyclerView-:沒有連接適配器;跳繩佈局
public class Appetizer extends AppCompatActivity {
private RecyclerView recyclerView;
private AdapterFood mAdapter;
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
mAdapter = new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Appetizer.this));
這是我的適配器類:
public class AdapterFood extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
DataFood current;
int currentPos=0;
public AdapterFood(Context context, List<DataFood> data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.list_item,parent,false);
Myholder holder=new Myholder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Myholder myholder=(Myholder) holder;
DataFood current=data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(current.foodId);
Glide.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(myholder.ivFood);
}
@Override
public int getItemCount() {
return data.size();
}
class Myholder extends RecyclerView.ViewHolder{
TextView textFoodname,textPrice,textId;
ImageView ivFood;
public Myholder(View itemView) {
super(itemView);
textFoodname=(TextView) itemView.findViewById(R.id.textFoodname);
ivFood=(ImageView)itemView.findViewById(R.id.ivFood);
textId=(TextView)itemView.findViewById(R.id.textid);
textPrice=(TextView) itemView.findViewById(R.id.textPrice);
}
}
}
我嘗試任何方法,但我無法解決探針。我做錯了什麼?
檢查'data.size == 0' –
組佈局管理器上面的設置適配器 – TUSHAR
,你在recyclerView設置適配器?如果您有異步請求,則必須在獲取數據刷新適配器後設置適配器爲空值。 –