Java 将映像从服务器传递到适配器而不保存在drawable中
我正在使用adapter/cardLayout/recycleView,我使用教程进行设置 一切正常,现在我需要下载一张图片(我使用毕加索)并在卡片布局中设置,问题是我需要在每张卡片上传递它:/ 以下是我目前的工作:Java 将映像从服务器传递到适配器而不保存在drawable中,java,android,android-volley,picasso,Java,Android,Android Volley,Picasso,我正在使用adapter/cardLayout/recycleView,我使用教程进行设置 一切正常,现在我需要下载一张图片(我使用毕加索)并在卡片布局中设置,问题是我需要在每张卡片上传递它:/ 以下是我目前的工作: public class SimiliarPhotos extends AppCompatActivity implements IResult { RecyclerView rv; LinearLayoutManager llm; ArrayList&l
public class SimiliarPhotos extends AppCompatActivity implements IResult {
RecyclerView rv;
LinearLayoutManager llm;
ArrayList<Plant> plants = new ArrayList<Plant>();
SimiliarPlantsAdapter adapter;
VolleyService mVolleyService;
IResult mResultCallback = null;
final String GETREQUEST = "GETCALL";
//login url connection
final String URL = "http://10.0.2.2:3000/plants";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_similiar_photos);
rv = (RecyclerView)findViewById(R.id.rv);
llm = new LinearLayoutManager(this);
llm.setAutoMeasureEnabled(true);
rv.setLayoutManager(llm);
initializeAdapter();
initVolleyCallback();
mVolleyService = new VolleyService(mResultCallback,this);
mVolleyService.getDataVolley(GETREQUEST,URL);
}
@Override
public void notifySuccess(String requestType, JSONObject response) {
Log.d("resposta",response.toString());
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
Log.d("resposta",response.toString());
}
@Override
public void notifyError(String requestType, VolleyError error) {
Log.d("resposta",error.toString());
}
void initVolleyCallback(){
mResultCallback = new IResult() {
@Override
public void notifySuccess(String requestType, JSONObject response) {
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
Plant plant;
Log.d("ENTERED","ENTEREDHERE1");
// iterate over the JSONArray response
for (int i=0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i); // get the individual object from JSONArray
int id = Integer.parseInt(object.getString("id")); // get the unique identifier from the object
String specie = object.getString("specie"); // get the name of the specie from the object
String description = object.getString("description"); // get the description of the object
ImageLoad(specie);
plant = new Plant(id,specie,description); // construct the object
Log.d("plant",String.valueOf(plant));
plants.add(plant); // add the object to the arraylist so it can be used on the cardLayout
} catch (JSONException e) {
Log.d("ENTERED",e.toString());
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
@Override
public void notifyError(String requestType, VolleyError error) {
Log.d("resposta",error.toString());
}
};
}
public void ImageLoad(String specie){
String urlFoto = "http://10.0.2.2:3000/images/" + specie + "/Thumbnail.jpg";
Picasso.with(this)
.load(urlFoto)
.into(imageView);
}
public void initializeAdapter(){
Log.d("plants",String.valueOf(plants.size()));
adapter = new SimiliarPlantsAdapter(plants);
rv.setAdapter(adapter);
}
}
public类SimiliarPhotos扩展AppCompatActivity实现IResult{
回收视图rv;
线性布局经理llm;
ArrayList plants=新的ArrayList();
类似植物适配器;
截击发球;
IResult mResultCallback=null;
最后一个字符串GETREQUEST=“GETCALL”;
//登录url连接
最终字符串URL=”http://10.0.2.2:3000/plants";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u similiar\u照片);
rv=(RecyclerView)findViewById(R.id.rv);
llm=新的直线布局经理(本);
llm.setAutoMeasureEnabled(真);
rv.setLayoutManager(llm);
初始化为apter();
initVolleyCallback();
mVolleyService=新的截击服务(mResultCallback,this);
getDataVolley(GETREQUEST,URL);
}
@凌驾
public void notifySuccess(字符串请求类型,JSONObject响应){
Log.d(“resposta”,response.toString());
}
@凌驾
public void notifySuccess(字符串请求类型,JSONArray响应){
Log.d(“resposta”,response.toString());
}
@凌驾
public void notifyError(字符串请求类型,截击错误){
Log.d(“resposta”,error.toString());
}
void initVolleyCallback(){
mResultCallback=new-IResult(){
@凌驾
public void notifySuccess(字符串请求类型,JSONObject响应){
}
@凌驾
public void notifySuccess(字符串请求类型,JSONArray响应){
植物;
日志d(“已输入”、“已输入此处1”);
//迭代JSONArray响应
对于(int i=0;i
如何将该图像传递到正确的卡中?任何提示?您需要通过适配器的
onBindViewHolder
方法中的Picasso
加载图像。查看我在适配器中执行的操作
public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.MyViewHolder> {
private List<OffersModel> list;
Context mContext;
public OffersAdapter(Context mContext, List<OffersModel> list) {
this.list = list;
this.mContext = mContext;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_offers, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
OffersModel offersModel = list.get(position);
holder.product.setText(offersModel.getProduct());
holder.description.setText(offersModel.getDiscription());
Picasso.with(mContext)
.load(offersModel.getImageurl())
.into(holder.offersimage);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView offersimage;
TextView product,description,category;
public MyViewHolder(View itemView) {
super(itemView);
offersimage = (ImageView) itemView.findViewById(R.id.offersimage);
product = (TextView) itemView.findViewById(R.id.offertitle);
description = (TextView) itemView.findViewById(R.id.offerdescription);
}
}
}
公共类OffersAdapter扩展了RecyclerView.Adapter{
私人名单;
语境;
PublicOfferAdapter(上下文mContext,列表){
this.list=列表;
this.mContext=mContext;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutFlater.from(parent.getContext()).flate(R.layout.cardview\u提供,parent,false);
返回新的MyViewHolder(itemView);
}
@凌驾
公共无效onBindViewHolder(最终MyViewHolder,内部位置){
OffersModel OffersModel=list.get(位置);
holder.product.setText(offersModel.getProduct());
holder.description.setText(offersModel.getdescription());
毕加索(McContext)
.load(OfferModel.getImageurl())
.进入(持有人、要约人形象);
}
@凌驾
public int getItemCount(){
返回list.size();
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
图像视图提供图像;
TextView产品、说明、类别;
公共MyViewHolder(查看项目视图){
超级(项目视图);
OfferImage=(ImageView)itemView.findViewById(R.id.OfferImage);
product=(TextView)itemView.findViewById(R.id.offertite);
description=(TextView)itemView.findViewById(R.id.offerdescription);
}
}
}
删除ImageLoad(字符串种类){..}
方法调用和实现。像这样创建适配器类
public class SimplePlants extends RecyclerView.Adapter<SimplePlants.MyViewHolder> {
private List<Plant> list;
Context mContext;
public SimplePlants(Context mContext, List<Plant> list) {
this.list = list;
this.mContext = mContext;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_single_plant_layout, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Plant plant = list.get(position);
holder.species.setText(plant.getSpecies ());
holder.description.setText(plant.getDiscription());
loadImage (holder.image, plant.getSpecies ());
}
public void loadImage(ImageView imageView, String specie){
String urlFoto = "http://10.0.2.2:3000/images/" + specie + "/Thumbnail.jpg";
Picasso.with(this)
.load(urlFoto)
.into(imageView);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView species,description;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
species = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
}
}
}
公共类SimplePlants扩展了RecyclerView.Adapter{
私人名单;
语境;
公共SimplePlants(上下文mContext,列表){
this.list=列表;
this.mContext=mContext;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutFlater.from(parent.getContext()).inflate