android 改装毕加索如何从 node.js 文件夹加载图像
android retrofit picasso how to load images from node.js folder
我已经实现了具有动态内容(图像除外)的 Bike 对象的回收视图
它工作正常,但现在我决定让我的图像动态化并将它们与我的对象一起显示在 recyclerview 中,我看到了关于毕加索的教程但我什么都不懂,我在 Bike 对象中有一个图像 属性 ,并且此 属性 包含“http://localhost:3000/images/bike.png”,例如,如何使用改装和毕加索将其图像加载到我的自行车上,我将展示我尝试过的内容,但是显然毕加索的语法在 BikeAdapter 中是错误的。
//自行车适配器:
package com.example.miniprojetandroid.adapters;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.miniprojetandroid.R;
import com.example.miniprojetandroid.models.Bike;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
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());
holder.BikeImage.setBackgroundResource(singleItem.getImage());
Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).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);
}
}
包含回收视图的我的片段
package com.example.miniprojetandroid.ui.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.miniprojetandroid.R;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.miniprojetandroid.Retrofit.BikeService;
import com.example.miniprojetandroid.Retrofit.RetrofitClient;
import com.example.miniprojetandroid.Retrofit.UserService;
import com.example.miniprojetandroid.adapters.BikesAdapter;
import com.example.miniprojetandroid.models.Bike;
import com.example.miniprojetandroid.models.User;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.squareup.picasso.*;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class FragmentOne extends Fragment implements BikesAdapter.Callback{
private BikeService apiService;
private RecyclerView recyclerView;
private List<Bike> bikes = new ArrayList<Bike>();
private BikesAdapter mAdapter;
List<Bike> result = new ArrayList<Bike>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container,false);
recyclerView = rootView.findViewById(R.id.recycler_bikes);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.VERTICAL, false));
apiService = RetrofitClient.getClient().create(BikeService.class);
mAdapter = new BikesAdapter(getActivity(), (ArrayList<Bike>) bikes);
fillData();
recyclerView.setAdapter(mAdapter);
mAdapter.setCallback(this);
return rootView;
}
public void fillData(){
Call<List<Bike>> call = apiService.getBikes();
call.enqueue(new Callback<List<Bike>>() {
@Override
public void onResponse(Call<List<Bike>> call, Response<List<Bike>> response) {
if(response.isSuccessful()){
bikes.addAll(response.body());
for(Bike bike: bikes){
bike.setImage(R.drawable.ruebike);
}
Log.e("Bike LIST", bikes.toString());
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<List<Bike>> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
/*bikes.add(new Bike(1,"ECO", "RTT" , "44", R.drawable.ruebike ));
bikes.add(new Bike(2,"AAA", "RTT" , "33", R.drawable.ruebike ));
bikes.add(new Bike(3,"BBB", "RUE" , "11", R.drawable.ruebike ));
bikes.add(new Bike(4,"EEE", "SPORT" , "25", R.drawable.ruebike ));
bikes.add(new Bike(5,"CCC", "SPORT" , "77", R.drawable.ruebike ));
Log.e("USERS LIST", bikes.toString());*/
}
@Override
public void onItemClicked(Bike bike) {
Bundle bundle = new Bundle();
bundle.putInt("bike_id",bike.getId());
bundle.putString("model", bike.getModel());
bundle.putString("type", bike.getType());
bundle.putString("price", bike.getPrice());
bundle.putInt("image", bike.getImage());
DetailsFragment f = new DetailsFragment();
f.setArguments(bundle);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentsContainer, f )
.commit();
}
}
我认为我的漏洞问题出在 BikeAdapter 的那个方法中
@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());
holder.BikeImage.setBackgroundResource(singleItem.getImage());
Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).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();
}
});
}
正好在这一行:
holder.BikeImage.setBackgroundResource(singleItem.getImage());
Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).into(holder.BikeImage);
如何加载我的图片???在毕加索语法中要更正什么?
//我的自行车型号
package com.example.miniprojetandroid.models;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Entity(tableName = "bike_table")
public class Bike {
@SerializedName("bike_id")
@Expose
@PrimaryKey(autoGenerate = true)
public int id;
@SerializedName("model")
@Expose
@ColumnInfo(name = "model")
public String model;
@SerializedName("type")
@Expose
@ColumnInfo(name = "type")
public String type;
@SerializedName("price")
@Expose
@ColumnInfo(name = "price")
public String price;
@ColumnInfo(name = "image")
public int image;
@Ignore
public Bike(){
}
@Ignore
public Bike( String model, int image) {
this.model = model;
this.image = image;
}
public Bike( String model, String type, String price, int image) {
this.model = model;
this.type = type;
this.price = price;
this.image = image;
}
@Ignore
public Bike(int id, String model, String price, String type) {
this.id = id;
this.model = model;
this.price = price;
this.type = type;
}
@Ignore
public Bike(int id, String model, String type, String price, int image) {
this.id = id;
this.model = model;
this.type = type;
this.price = price;
this.image = image;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setModel(String model) {
this.model = model;
}
public void setType(String type) {
this.type = type;
}
public void setImage(int image) {
this.image = image;
}
public String getModel() {
return model;
}
public String getType() {
return type;
}
public int getImage() {
return image;
}
@Override
public String toString() {
return "Bike{" +
"id=" + id +
", model='" + model + '\'' +
", type='" + type + '\'' +
", price='" + price + '\'' +
", image=" + image +
'}';
}
}
去掉这个。
holder.BikeImage.setBackgroundResource(singleItem.getImage());
并使用这个
Picasso.get().load(singleItem.getImage()).into(holder.BikeImage);
我已经实现了具有动态内容(图像除外)的 Bike 对象的回收视图 它工作正常,但现在我决定让我的图像动态化并将它们与我的对象一起显示在 recyclerview 中,我看到了关于毕加索的教程但我什么都不懂,我在 Bike 对象中有一个图像 属性 ,并且此 属性 包含“http://localhost:3000/images/bike.png”,例如,如何使用改装和毕加索将其图像加载到我的自行车上,我将展示我尝试过的内容,但是显然毕加索的语法在 BikeAdapter 中是错误的。
//自行车适配器:
package com.example.miniprojetandroid.adapters;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.miniprojetandroid.R;
import com.example.miniprojetandroid.models.Bike;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
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());
holder.BikeImage.setBackgroundResource(singleItem.getImage());
Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).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);
}
}
包含回收视图的我的片段
package com.example.miniprojetandroid.ui.fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.miniprojetandroid.R;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.miniprojetandroid.Retrofit.BikeService;
import com.example.miniprojetandroid.Retrofit.RetrofitClient;
import com.example.miniprojetandroid.Retrofit.UserService;
import com.example.miniprojetandroid.adapters.BikesAdapter;
import com.example.miniprojetandroid.models.Bike;
import com.example.miniprojetandroid.models.User;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.squareup.picasso.*;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class FragmentOne extends Fragment implements BikesAdapter.Callback{
private BikeService apiService;
private RecyclerView recyclerView;
private List<Bike> bikes = new ArrayList<Bike>();
private BikesAdapter mAdapter;
List<Bike> result = new ArrayList<Bike>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container,false);
recyclerView = rootView.findViewById(R.id.recycler_bikes);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),
LinearLayoutManager.VERTICAL, false));
apiService = RetrofitClient.getClient().create(BikeService.class);
mAdapter = new BikesAdapter(getActivity(), (ArrayList<Bike>) bikes);
fillData();
recyclerView.setAdapter(mAdapter);
mAdapter.setCallback(this);
return rootView;
}
public void fillData(){
Call<List<Bike>> call = apiService.getBikes();
call.enqueue(new Callback<List<Bike>>() {
@Override
public void onResponse(Call<List<Bike>> call, Response<List<Bike>> response) {
if(response.isSuccessful()){
bikes.addAll(response.body());
for(Bike bike: bikes){
bike.setImage(R.drawable.ruebike);
}
Log.e("Bike LIST", bikes.toString());
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<List<Bike>> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
/*bikes.add(new Bike(1,"ECO", "RTT" , "44", R.drawable.ruebike ));
bikes.add(new Bike(2,"AAA", "RTT" , "33", R.drawable.ruebike ));
bikes.add(new Bike(3,"BBB", "RUE" , "11", R.drawable.ruebike ));
bikes.add(new Bike(4,"EEE", "SPORT" , "25", R.drawable.ruebike ));
bikes.add(new Bike(5,"CCC", "SPORT" , "77", R.drawable.ruebike ));
Log.e("USERS LIST", bikes.toString());*/
}
@Override
public void onItemClicked(Bike bike) {
Bundle bundle = new Bundle();
bundle.putInt("bike_id",bike.getId());
bundle.putString("model", bike.getModel());
bundle.putString("type", bike.getType());
bundle.putString("price", bike.getPrice());
bundle.putInt("image", bike.getImage());
DetailsFragment f = new DetailsFragment();
f.setArguments(bundle);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentsContainer, f )
.commit();
}
}
我认为我的漏洞问题出在 BikeAdapter 的那个方法中
@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());
holder.BikeImage.setBackgroundResource(singleItem.getImage());
Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).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();
}
});
}
正好在这一行:
holder.BikeImage.setBackgroundResource(singleItem.getImage()); Picasso.with(mContext).load("http://localhost:3000/images/bike.png").placeholder( ?).into(holder.BikeImage);
如何加载我的图片???在毕加索语法中要更正什么?
//我的自行车型号
package com.example.miniprojetandroid.models;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Entity(tableName = "bike_table")
public class Bike {
@SerializedName("bike_id")
@Expose
@PrimaryKey(autoGenerate = true)
public int id;
@SerializedName("model")
@Expose
@ColumnInfo(name = "model")
public String model;
@SerializedName("type")
@Expose
@ColumnInfo(name = "type")
public String type;
@SerializedName("price")
@Expose
@ColumnInfo(name = "price")
public String price;
@ColumnInfo(name = "image")
public int image;
@Ignore
public Bike(){
}
@Ignore
public Bike( String model, int image) {
this.model = model;
this.image = image;
}
public Bike( String model, String type, String price, int image) {
this.model = model;
this.type = type;
this.price = price;
this.image = image;
}
@Ignore
public Bike(int id, String model, String price, String type) {
this.id = id;
this.model = model;
this.price = price;
this.type = type;
}
@Ignore
public Bike(int id, String model, String type, String price, int image) {
this.id = id;
this.model = model;
this.type = type;
this.price = price;
this.image = image;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setModel(String model) {
this.model = model;
}
public void setType(String type) {
this.type = type;
}
public void setImage(int image) {
this.image = image;
}
public String getModel() {
return model;
}
public String getType() {
return type;
}
public int getImage() {
return image;
}
@Override
public String toString() {
return "Bike{" +
"id=" + id +
", model='" + model + '\'' +
", type='" + type + '\'' +
", price='" + price + '\'' +
", image=" + image +
'}';
}
}
去掉这个。
holder.BikeImage.setBackgroundResource(singleItem.getImage());
并使用这个
Picasso.get().load(singleItem.getImage()).into(holder.BikeImage);