gpt4 book ai didi

java - Picasso View 无法转换为 DataAdapter 内的 Context

转载 作者:行者123 更新时间:2023-12-02 13:37:37 25 4
gpt4 key购买 nike

我创建了我的应用程序,它正在连接到我的服务器上的 JSON 文件以获取有关用户的信息,然后将其设置到卡行上。这一切都工作正常,但我无法让 Picasso 使用它,除非我将它放在我的公共(public) ViewHolder(View view) 方法中,但随后我无法访问该数组。有没有什么办法解决这一问题?我收到错误

Error:(55, 22) error: incompatible types: DataAdapter.ViewHolder cannot be converted to Context

代码贴在下面。谢谢。

package com.example.curtisboylan.myapplication;

/**
* Created by curtisboylan on 02/02/2017.
*/
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> username;
private ArrayList<String> userid;
private ArrayList<String> location;
private ArrayList<String> reviewscore;
private ArrayList<String> price;
private ArrayList<String> url;


public DataAdapter(ArrayList<String> username, ArrayList<String> userid, ArrayList<String> location, ArrayList<String> reviewscore, ArrayList<String> price, ArrayList<String> url) {
this.username = username;
this.userid = userid;
this.location = location;
this.reviewscore = reviewscore;
this.price = price;
this.url = url;

}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);

return new ViewHolder(view);

}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
final String item = userid.get(i);
final String usernamepass = username.get(i);
viewHolder.usernametext.setText(username.get(i));
viewHolder.reviewscoretext.setText(reviewscore.get(i));
viewHolder.pricetext.setText("€" + price.get(i) +"/hr");
viewHolder.locationtext.setText(location.get(i));
//"https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg";

Picasso.with(viewHolder)
.load(url.get(i))
.into(viewHolder.profileimg);






viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "User ID: " + item, Toast.LENGTH_SHORT).show();

Intent intent=new Intent(view.getContext(),TechnicianProfile.class);
intent.putExtra("username", usernamepass);
intent.putExtra("userid", item);
view.getContext().startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return username.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView usernametext;
private TextView pricetext;
private TextView locationtext;
private TextView reviewscoretext;
private ImageView profileimg;

public ViewHolder(View view) {
super(view);

usernametext = (TextView)view.findViewById(R.id.textView4);
pricetext = (TextView)view.findViewById(R.id.price);
locationtext = (TextView)view.findViewById(R.id.location);
reviewscoretext = (TextView)view.findViewById(R.id.reviewscore);
profileimg = (ImageView)view.findViewById(R.id.userpic);

}
}

}

最佳答案

您应该通过 Context

new DataAdapter(..,...,CurrentActivity.this)//传递上下文

     Context context;//Global 
public DataAdapter(ArrayList<String> username, ArrayList<String> userid, ArrayList<String> location, ArrayList<String> reviewscore, ArrayList<String> price, ArrayList<String> url,Context context) {

this.context = context;

}

现在

 Picasso.with(context)

关于java - Picasso View 无法转换为 DataAdapter 内的 Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com