我正在开发一个简单的应用程序,在那里我搜索电视节目,其中应用程序生成对TMDB的GET请求。从那里我试图在cardview中显示结果的图像和细节。我使用Picasso从TMDB加载图像,它工作正常。问题是当我滚动时,首先加载的图像会刷新到其他图像视图而不是那些显示特定图像。
这是我的asynctask代码
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
答案 0 :(得分:3)
在bindView()
中添加PersonViewHolder
,如下所示
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()
将列表中的每个元素与适当的元素绑定。
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
try {
personViewHolder.bindView(i);
} catch(Exception e) {e.printStackTrace();}
}