gpt4 book ai didi

android - Picasso 和 Recyclerview 的问题 - 图像在滚动时刷新但在错误的 ImageView 中

转载 作者:行者123 更新时间:2023-11-29 17:24:16 25 4
gpt4 key购买 nike

我正在开发一个简单的应用程序,我在其中搜索电视节目,该应用程序在其中生成对 TMDB 的 GET 请求。从那里我试图在卡片 View 中显示结果的图像和详细信息。我正在使用 Picasso 从 TMDB 加载图像,它工作正常。问题是当我滚动时,首先加载的图像会刷新到其他 ImageView ,而不是那些显示特定图像的 ImageView 。

这是我的异步任务代码

class JsonTask extends AsyncTask<String,String,String[][]>{
private List<FeedItem> persons;
String [][] Finalarray = new String[200][3];
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected String[][] doInBackground(String... params) {
HttpURLConnection urlConnection = null;
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader reader = null;
try{
URL Request1 = new URL(params[0]);
urlConnection = (HttpURLConnection) Request1.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
Log.d("DEBUG_TAG", "The response code is: " + responseCode + " " + urlConnection.getResponseMessage());
InputStream in = urlConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine())!=null){
Log.i("check","Line is"+line);
buffer.append(line);
}
String jsonobjectt= buffer.toString();
JSONObject parentJson = new JSONObject(jsonobjectt);
JSONArray parentArray = (JSONArray) parentJson.get("results");
dataobtained=parentJson.getInt("total_results");
for(int i = 0; i<parentJson.getInt("total_results"); i++){
JSONObject temp = parentArray.getJSONObject(i);
Finalarray[i][0] = temp.getString("original_name");
Finalarray[i][1] = temp.getString("first_air_date");
Finalarray[i][2] = temp.getString("poster_path");
}
}
catch (MalformedURLException e){
Log.i("I do", "eee1");
e.printStackTrace();
} catch (IOException e) {
Log.i("I do", "eee2");
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
Log.i("I do", "eee3");
if(urlConnection!=null)
urlConnection.disconnect();
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
Log.i("I do", "eee4");
e.printStackTrace();
}
}
return Finalarray;
}

@Override
protected void onPostExecute(String[][] result) {
persons = new ArrayList<>();
for(int i=0;i<dataobtained;i++){
//if(!result[i][0].isEmpty()&&result[i][2]!=null)
persons.add(new FeedItem(result[i][0], result[i][1],"http://image.tmdb.org/t/p/w500"+result[i][2],searchpagee.this));
}
new Thread(new Runnable() {
@Override
public void run() {

}
}).start();
mRecyclerView.setLayoutManager(llm);
RVAdapter adapter = new RVAdapter(persons);
mRecyclerView.setAdapter(adapter);
}
}
}

这是我的 Recycleradapter 代码

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{

List<FeedItem> persons;

RVAdapter(List<FeedItem> persons){
this.persons = persons;
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
public static ImageView personPhoto;
CardView cv;
TextView personName;
TextView personAge;

PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.titleCir);
personAge = (TextView)itemView.findViewById(R.id.contentsCir);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardv, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).title);
personViewHolder.personAge.setText(persons.get(i).content);
Picasso.with(persons.get(i).context).load(persons.get(i).photoId).placeholder(R.drawable.placeholder).fit().into(PersonViewHolder.personPhoto);
}
@Override
public int getItemCount() {
return persons == null ? 0 : persons.size();
}

This is before scrolling While scrolling. Images get repeated

最佳答案

像这样在您的 PersonViewHolder 中添加一个 bindView()

public static class PersonViewHolder extends RecyclerView.ViewHolder {
public static ImageView personPhoto;
CardView cv;
TextView personName;
TextView personAge;

PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.titleCir);
personAge = (TextView)itemView.findViewById(R.id.contentsCir);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}

public void bindView(int i) {
personName.setText(persons.get(i).title);
personAge.setText(persons.get(i).content);
Picasso.with(persons.get(i).context).load(persons.get(i).photoId).placeholder(R.drawable.placeholder).fit().into(personPhoto);
}
}

并且在 bindViewHolder 中,您需要调用 bindView() 以将列表中的每个元素与适当的元素绑定(bind)。

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
try {
personViewHolder.bindView(i);
} catch(Exception e) {e.printStackTrace();}
}

关于android - Picasso 和 Recyclerview 的问题 - 图像在滚动时刷新但在错误的 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251023/

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