图像未显示在 RecyclerView 中
Images are not displaying in RecyclerView
我已经用后端 node.js mysql 实现了 RecyclerView
并且我已经为图像设置了一个文件夹。在 android 中,RecyclerView
工作正常,但是当我决定使用 url 中的动态图像时,它们不会在 RecyclerView
中显示,我正在从 JSON.
{
"location_id": 3,
"datelocation": "15/10/2020 15:11",
"adresselocation": "Klibia",
"bike_id": 3,
"user_id": 10,
"model": "Skygrey",
"type": "RTT",
"price": "55",
"image": "http://localhost:3000/bike2.png"
}
我的适配器是这样设置的:
public class BikesAdapter extends RecyclerView.Adapter<BikesAdapter.BikesViewHolder> {
private final ArrayList<Bike> bikes;
private Context mContext;
private Callback mCallback;
public BikesAdapter(Context mContext, ArrayList<Bike> bikes) {
this.mContext = mContext;
this.bikes = bikes;
}
@NonNull
@Override
public BikesAdapter.BikesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mItemView = LayoutInflater.from(mContext).inflate(R.layout.bike_list_item, parent, false);
return new BikesViewHolder(mItemView, this);
}
@Override
public void onBindViewHolder(@NonNull BikesAdapter.BikesViewHolder holder, int position) {
final Bike singleItem = bikes.get(position);
holder.BikeName.setText(singleItem.getModel());
Picasso.with(mContext).load(singleItem.getImage()).resize(50, 50).into(holder.BikeImage);
holder.bikeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onItemClicked(singleItem);
}
});
holder.BikeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, singleItem.getModel(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return bikes.size();
}
public class BikesViewHolder extends RecyclerView.ViewHolder {
public final TextView BikeName;
public final ImageView BikeImage;
public final Button bikeView;
final BikesAdapter mAdapter;
public BikesViewHolder(@NonNull View itemView, BikesAdapter mAdapter) {
super(itemView);
this.BikeName = itemView.findViewById(R.id.bikeName);
this.BikeImage = itemView.findViewById(R.id.bikeImage);
this.bikeView = itemView.findViewById(R.id.btn_view);
this.mAdapter = mAdapter;
}
}
public void setCallback(Callback callback) {
mCallback = callback;
}
public interface Callback {
void onItemClicked(Bike bike);
}
}
这里有什么问题?为什么图像没有显示在我的 RecyclerView
中?
我试过 Picasso,就像你在 BikeAdapter
中看到的 onBindViewHolder()
方法一样,我缺少什么我不明白!
首先,我们称呼毕加索的方式发生了变化。
Picasso.get().load("imageUrl").into(imageView);
还要检查您的清单中是否有互联网许可
<uses-permission android:name="android.permission.INTERNET"/>
最后尝试打开Picasso logs看看有没有报错
Picasso.get().setLoggingEnabled(true)
以防万一,请确保您的图像 url 在浏览器上正确加载。
我已经用后端 node.js mysql 实现了 RecyclerView
并且我已经为图像设置了一个文件夹。在 android 中,RecyclerView
工作正常,但是当我决定使用 url 中的动态图像时,它们不会在 RecyclerView
中显示,我正在从 JSON.
{
"location_id": 3,
"datelocation": "15/10/2020 15:11",
"adresselocation": "Klibia",
"bike_id": 3,
"user_id": 10,
"model": "Skygrey",
"type": "RTT",
"price": "55",
"image": "http://localhost:3000/bike2.png"
}
我的适配器是这样设置的:
public class BikesAdapter extends RecyclerView.Adapter<BikesAdapter.BikesViewHolder> {
private final ArrayList<Bike> bikes;
private Context mContext;
private Callback mCallback;
public BikesAdapter(Context mContext, ArrayList<Bike> bikes) {
this.mContext = mContext;
this.bikes = bikes;
}
@NonNull
@Override
public BikesAdapter.BikesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mItemView = LayoutInflater.from(mContext).inflate(R.layout.bike_list_item, parent, false);
return new BikesViewHolder(mItemView, this);
}
@Override
public void onBindViewHolder(@NonNull BikesAdapter.BikesViewHolder holder, int position) {
final Bike singleItem = bikes.get(position);
holder.BikeName.setText(singleItem.getModel());
Picasso.with(mContext).load(singleItem.getImage()).resize(50, 50).into(holder.BikeImage);
holder.bikeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onItemClicked(singleItem);
}
});
holder.BikeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, singleItem.getModel(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return bikes.size();
}
public class BikesViewHolder extends RecyclerView.ViewHolder {
public final TextView BikeName;
public final ImageView BikeImage;
public final Button bikeView;
final BikesAdapter mAdapter;
public BikesViewHolder(@NonNull View itemView, BikesAdapter mAdapter) {
super(itemView);
this.BikeName = itemView.findViewById(R.id.bikeName);
this.BikeImage = itemView.findViewById(R.id.bikeImage);
this.bikeView = itemView.findViewById(R.id.btn_view);
this.mAdapter = mAdapter;
}
}
public void setCallback(Callback callback) {
mCallback = callback;
}
public interface Callback {
void onItemClicked(Bike bike);
}
}
这里有什么问题?为什么图像没有显示在我的 RecyclerView
中?
我试过 Picasso,就像你在 BikeAdapter
中看到的 onBindViewHolder()
方法一样,我缺少什么我不明白!
首先,我们称呼毕加索的方式发生了变化。
Picasso.get().load("imageUrl").into(imageView);
还要检查您的清单中是否有互联网许可
<uses-permission android:name="android.permission.INTERNET"/>
最后尝试打开Picasso logs看看有没有报错
Picasso.get().setLoggingEnabled(true)
以防万一,请确保您的图像 url 在浏览器上正确加载。