我想显示电子通讯应用程序的产品图片,请让我知道我的代码有什么问题,因为它没有在Recycler视图中显示图像。
RecyclerAdapter类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Record_> mitems = new ArrayList<>();
private LayoutInflater layoutInflater;
private Context mcontext;
public MyAdapter(ArrayList<Record_> mitems, LayoutInflater layoutInflater, Context mcontext) {
this.mitems = mitems;
this.layoutInflater = layoutInflater;
this.mcontext = mcontext;
}
public MyAdapter(Context context, ArrayList<Record_> items) {
this.layoutInflater = LayoutInflater.from((Context) context);
this.mitems = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.from(parent.getContext()).inflate(R.layout.item_display_layout,parent,false);
/*ImageView imageView = new ImageView(mcontext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
));*/
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Picasso.with(mcontext).load(mitems.get(position).getProductImageUrl());
//holder.imageView.setImageURI(Uri.parse(mitems.get(position).getProductImageUrl()));
//Picasso.with(holder.imageView.getContext());
Picasso.with(holder.imageView.getContext()).load(mitems.get(position).getProductImageUrl()).fit().centerCrop().into(holder.imageView);
holder.mtextView.setText(mitems.get(position).getProductDescription() );
holder.priceTv.setText("$"+mitems.get(position).getSkuFinalPrice());
}
@Override
public int getItemCount() {
return mitems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mtextView, priceTv;
public ImageView imageView;
public ViewHolder(View view){
super(view);
imageView = view.findViewById(R.id.ivProduct);
mtextView = view.findViewById(R.id.descriptionTV);
priceTv = view.findViewById(R.id.priceTV);
}
ProductDisplayActivity.java
public class ProductDisplayActivity extends AppCompatActivity{
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_display);
recyclerView = findViewById(R.id.products);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
getProducts(pod);
private void getProducts(String id) {
CategoryInterface productInterface =RetrObj.getRetro().create(CategoryInterface.class);
Call<ProductRespose> productCall = productInterface.getRecords(id);
//productCall = productInterface.getProductImageUrl(id);
productCall.enqueue(new Callback<ProductRespose>() {
@Override
public void onResponse(Call<ProductRespose> call, Response<ProductRespose> response) {
int statusCode = response.code();
if(statusCode==HttpURLConnection.HTTP_OK){
ProductRespose productRecord = response.body();
myAdapter = new MyAdapter(ProductDisplayActivity.this,productRecord.getRecords().get(0).getAttributes().getRecords());
recyclerView.setAdapter(myAdapter);
// recyclerView.notify();
}
}
@Override
public void onFailure(Call<ProductRespose> call, Throwable t) {
Toast.makeText(ProductDisplayActivity.this, "Something Went Wrong", Toast.LENGTH_SHORT).show();
}
});
}
}
我使用Retrofit从API获取数据,该列表显示在回收器视图中,但图像不是来自产品。我用毕加索来显示图像,但请告诉我错误。
答案 0 :(得分:0)
为了获得错误,您可以尝试将其放入适配器中,然后检查logcat:
Picasso.Builder builder = new Picasso.Builder(this);
builder.listener(new Picasso.Listener()
{
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
{
exception.printStackTrace();
}
});
builder.build().load(URL).into(imageView);