照片未出现在 RecyclerView 中
Photos not appearing in RecyclerView
我正在尝试在回收站视图中添加带有标题的 flickr 图像我遇到的问题是我想在循环中将图像及其标题传递给 FlickrPhoto class(请看上面看看我在说什么)如果你注意到这一行 photos.add(new FlickrPhoto(title,uri));你会在后台任务的入队函数中看到它在循环内,所以我认识到该行没有执行..但是当我在入队函数之后尝试它对我有用但当然只适用于一个图像因为,我在循环之外。我尝试了很多解决方案,但都失败了
这是我的照片列表Activity
package com.example.karim.bluecrunch;
/**
* Created by karim on 8/26/16.
*/
public class FlickrPhoto {
String title , image ;
public FlickrPhoto(String title , String image )
{
this.image = image;
this.title = title;
}
}
这是我的 PhotosRecyclerViewAdapter
package com.example.karim.bluecrunch;
public class PhotosRecyclerViewAdapter extends RecyclerView.Adapter<PhotosRecyclerViewAdapter.PhotoViewHolder> {
List<FlickrPhoto> photos = Collections.emptyList();
Context context;
LayoutInflater inflater;
public PhotosRecyclerViewAdapter(Context context , List<FlickrPhoto> photos)
{
this.context = context;
this.photos = photos;
inflater = LayoutInflater.from(context);
}
@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = inflater.inflate(R.layout.single_row,parent,false);
return new PhotoViewHolder(row);
}
@Override
public void onBindViewHolder(PhotoViewHolder holder, int position) {
FlickrPhoto photo = photos.get(position);
holder.textView.setText(photo.title);
Uri uri = Uri.parse(photo.image);
Glide.with(context).load(uri).placeholder(R.drawable.image_placeholder).crossFade().into(holder.imageView);
}
@Override
public int getItemCount() {
return photos.size();
}
public class PhotoViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public PhotoViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.photoTitle);
imageView = (ImageView) itemView.findViewById(R.id.flickrPhoto);
}
}
}
这是我的照片列表Activity
package com.example.karim.bluecrunch;
public class PhotosListActivity extends AppCompatActivity {
List<FlickrPhoto> photos ;
ArrayList<String> photosTitles;
ArrayList<String> photoURLS;
String title;
String uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos_list);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
recyclerView.setAdapter(adapter);
}
public List<FlickrPhoto> getPhotos()
{
photos = new ArrayList<>();
final String API_KEY = "fdac2e9676991ac53b34651adab52518";
final String METHOD = "flickr.photos.search";
final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
final String FORMAT = "json";
final int CALL_BACK = 1;
HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
call.enqueue(new Callback<Photos>() {
@Override
public void onResponse(Call<Photos> call, Response<Photos> response) {
Log.d("MainActivity", "Status Code = " + response.code());
PhotosRetrofit photosRetrofit = response.body().photos;
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
title= photosRetrofit.getPhoto().get(i).getTitle();
photos.add(new FlickrPhoto(title,uri));
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
}
@Override
public void onFailure(Call<Photos> call, Throwable t) {
Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
Log.w("---___--- Error ",t.getMessage());
}
});
/* Log.w("Hello",uri.toString());
Log.w("Hello",title.toString());*/
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
return photos;
}
}
一切正常,但我的 Activity
中这一行的主要问题
photos.add(new FlickrPhoto(title,uri));
我认为不能在后台任务中完成,因为这一行
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
入队功能后工作正常,但我不知道如何使用这种技巧来解决这个问题。
编辑
请注意循环
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++)
{
uri="https://farm"... ;
title= ..;
**photos.add(new FlickrPhoto(title,uri));**
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+photosRetrofit.getPhoto().get(i).getServer()+"/"+photosRetrofit.getPhoto().get(i).getId()+"_" +photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
不会跳过,因为,如果您尝试在循环内记录标题或 uri,它将在 Logcat 中成功显示。主要问题是这行代码 photos.add(new FlickrPhoto(title,uri));
将数据 "title and uri " 传递给 FlickrPhoto class 的构造函数并且此操作在入队 -> onResponse(后台任务)中,我不不知道为什么它不起作用
将数据(图片和标题)加载到回收站视图中是例外,但实际上没有任何反应
我已经尝试了一些东西
首先,在 return photos() arrayList at the PhotosListActivity class 之前,我尝试将固定数据传递给 FlickrPhoto class 的构造函数它有效,图像显示在回收站视图中
其次,我尝试为 FlickrPhoto class 添加一个默认构造函数和 setter,getter 作为标题和图像,并在 PhotosList[=56] 的 OnCreate 函数上对其进行初始化=] class 然后在队列中我使用了我的 setter 和 getter 并且它可以正常工作并成功加载所有数据但是当我再次尝试 运行 时一切都消失了! :( ,我不知道为什么
做 setters 和 getters 是这样的
FlickrPhoto.java class
package com.example.karim.bluecrunch;
/**
* Created by karim on 8/26/16.
*/
public class FlickrPhoto {
String title , image ;
public FlickrPhoto(){}
public FlickrPhoto(String title , String image )
{
this.image = image;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
和 PostListActivity Class
package com.example.karim.bluecrunch;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.Url;
public class PhotosListActivity extends AppCompatActivity {
List<FlickrPhoto> photos ;
ArrayList<String> photosTitles;
ArrayList<String> photoURLS;
FlickrPhoto flickrPhoto ;
String title;
String uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos_list);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
recyclerView.setAdapter(adapter);
flickrPhoto = new FlickrPhoto();
}
public List<FlickrPhoto> getPhotos()
{
photos = new ArrayList<>();
final String API_KEY = "fdac2e9676991ac53b34651adab52518";
final String METHOD = "flickr.photos.search";
final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
final String FORMAT = "json";
final int CALL_BACK = 1;
HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
call.enqueue(new Callback<Photos>() {
@Override
public void onResponse(Call<Photos> call, Response<Photos> response) {
Log.d("MainActivity", "Status Code = " + response.code());
PhotosRetrofit photosRetrofit = response.body().photos;
//photosRetrofit.getPhoto().size()
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
title= photosRetrofit.getPhoto().get(i).getTitle();
flickrPhoto.setImage(uri);
flickrPhoto.setTitle(title);
photos.add(new FlickrPhoto(flickrPhoto.getTitle(),flickrPhoto.getImage()));
Log.w("++++++++++++",photosRetrofit.getPhoto().get(i).getTitle());
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
}
@Override
public void onFailure(Call<Photos> call, Throwable t) {
Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
Log.w("---___--- Error ",t.getMessage());
}
});
/* Log.w("Hello",uri.toString());
Log.w("Hello",title.toString());*/
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
return photos;
}
}
问题是 RecyclerView
在将元素及其数据添加到数组列表后不会自动更新。您需要在适配器上调用 notifyDataSetChanged()
以强制执行此更新。
我正在尝试在回收站视图中添加带有标题的 flickr 图像我遇到的问题是我想在循环中将图像及其标题传递给 FlickrPhoto class(请看上面看看我在说什么)如果你注意到这一行 photos.add(new FlickrPhoto(title,uri));你会在后台任务的入队函数中看到它在循环内,所以我认识到该行没有执行..但是当我在入队函数之后尝试它对我有用但当然只适用于一个图像因为,我在循环之外。我尝试了很多解决方案,但都失败了
这是我的照片列表Activity
package com.example.karim.bluecrunch;
/**
* Created by karim on 8/26/16.
*/
public class FlickrPhoto {
String title , image ;
public FlickrPhoto(String title , String image )
{
this.image = image;
this.title = title;
}
}
这是我的 PhotosRecyclerViewAdapter
package com.example.karim.bluecrunch;
public class PhotosRecyclerViewAdapter extends RecyclerView.Adapter<PhotosRecyclerViewAdapter.PhotoViewHolder> {
List<FlickrPhoto> photos = Collections.emptyList();
Context context;
LayoutInflater inflater;
public PhotosRecyclerViewAdapter(Context context , List<FlickrPhoto> photos)
{
this.context = context;
this.photos = photos;
inflater = LayoutInflater.from(context);
}
@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = inflater.inflate(R.layout.single_row,parent,false);
return new PhotoViewHolder(row);
}
@Override
public void onBindViewHolder(PhotoViewHolder holder, int position) {
FlickrPhoto photo = photos.get(position);
holder.textView.setText(photo.title);
Uri uri = Uri.parse(photo.image);
Glide.with(context).load(uri).placeholder(R.drawable.image_placeholder).crossFade().into(holder.imageView);
}
@Override
public int getItemCount() {
return photos.size();
}
public class PhotoViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public PhotoViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.photoTitle);
imageView = (ImageView) itemView.findViewById(R.id.flickrPhoto);
}
}
}
这是我的照片列表Activity
package com.example.karim.bluecrunch;
public class PhotosListActivity extends AppCompatActivity {
List<FlickrPhoto> photos ;
ArrayList<String> photosTitles;
ArrayList<String> photoURLS;
String title;
String uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos_list);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
recyclerView.setAdapter(adapter);
}
public List<FlickrPhoto> getPhotos()
{
photos = new ArrayList<>();
final String API_KEY = "fdac2e9676991ac53b34651adab52518";
final String METHOD = "flickr.photos.search";
final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
final String FORMAT = "json";
final int CALL_BACK = 1;
HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
call.enqueue(new Callback<Photos>() {
@Override
public void onResponse(Call<Photos> call, Response<Photos> response) {
Log.d("MainActivity", "Status Code = " + response.code());
PhotosRetrofit photosRetrofit = response.body().photos;
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
title= photosRetrofit.getPhoto().get(i).getTitle();
photos.add(new FlickrPhoto(title,uri));
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
}
@Override
public void onFailure(Call<Photos> call, Throwable t) {
Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
Log.w("---___--- Error ",t.getMessage());
}
});
/* Log.w("Hello",uri.toString());
Log.w("Hello",title.toString());*/
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
return photos;
}
}
一切正常,但我的 Activity
中这一行的主要问题photos.add(new FlickrPhoto(title,uri));
我认为不能在后台任务中完成,因为这一行
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
入队功能后工作正常,但我不知道如何使用这种技巧来解决这个问题。
编辑
请注意循环
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++)
{
uri="https://farm"... ;
title= ..;
**photos.add(new FlickrPhoto(title,uri));**
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+photosRetrofit.getPhoto().get(i).getServer()+"/"+photosRetrofit.getPhoto().get(i).getId()+"_" +photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
不会跳过,因为,如果您尝试在循环内记录标题或 uri,它将在 Logcat 中成功显示。主要问题是这行代码 photos.add(new FlickrPhoto(title,uri));
将数据 "title and uri " 传递给 FlickrPhoto class 的构造函数并且此操作在入队 -> onResponse(后台任务)中,我不不知道为什么它不起作用
将数据(图片和标题)加载到回收站视图中是例外,但实际上没有任何反应
我已经尝试了一些东西
首先,在 return photos() arrayList at the PhotosListActivity class 之前,我尝试将固定数据传递给 FlickrPhoto class 的构造函数它有效,图像显示在回收站视图中
其次,我尝试为 FlickrPhoto class 添加一个默认构造函数和 setter,getter 作为标题和图像,并在 PhotosList[=56] 的 OnCreate 函数上对其进行初始化=] class 然后在队列中我使用了我的 setter 和 getter 并且它可以正常工作并成功加载所有数据但是当我再次尝试 运行 时一切都消失了! :( ,我不知道为什么
做 setters 和 getters 是这样的 FlickrPhoto.java class
package com.example.karim.bluecrunch;
/**
* Created by karim on 8/26/16.
*/
public class FlickrPhoto {
String title , image ;
public FlickrPhoto(){}
public FlickrPhoto(String title , String image )
{
this.image = image;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
和 PostListActivity Class
package com.example.karim.bluecrunch;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.Url;
public class PhotosListActivity extends AppCompatActivity {
List<FlickrPhoto> photos ;
ArrayList<String> photosTitles;
ArrayList<String> photoURLS;
FlickrPhoto flickrPhoto ;
String title;
String uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos_list);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
recyclerView.setAdapter(adapter);
flickrPhoto = new FlickrPhoto();
}
public List<FlickrPhoto> getPhotos()
{
photos = new ArrayList<>();
final String API_KEY = "fdac2e9676991ac53b34651adab52518";
final String METHOD = "flickr.photos.search";
final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
final String FORMAT = "json";
final int CALL_BACK = 1;
HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
call.enqueue(new Callback<Photos>() {
@Override
public void onResponse(Call<Photos> call, Response<Photos> response) {
Log.d("MainActivity", "Status Code = " + response.code());
PhotosRetrofit photosRetrofit = response.body().photos;
//photosRetrofit.getPhoto().size()
for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
title= photosRetrofit.getPhoto().get(i).getTitle();
flickrPhoto.setImage(uri);
flickrPhoto.setTitle(title);
photos.add(new FlickrPhoto(flickrPhoto.getTitle(),flickrPhoto.getImage()));
Log.w("++++++++++++",photosRetrofit.getPhoto().get(i).getTitle());
Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
photosRetrofit.getPhoto().get(i).getServer()+"/"+
photosRetrofit.getPhoto().get(i).getId()+"_" +
photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
}
}
@Override
public void onFailure(Call<Photos> call, Throwable t) {
Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
Log.w("---___--- Error ",t.getMessage());
}
});
/* Log.w("Hello",uri.toString());
Log.w("Hello",title.toString());*/
photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
return photos;
}
}
问题是 RecyclerView
在将元素及其数据添加到数组列表后不会自动更新。您需要在适配器上调用 notifyDataSetChanged()
以强制执行此更新。