所以我正在尝试加载我在Parse服务器上的一些缩略图,但它不是很有效。
我遇到的问题是在for循环之外将图像加载到GridView中。由于某种原因,未在列表中添加位图。
这是我的代码:
private void getUserThumbs(){
Log.i("thumbs", "ThumbnailsFragment executed.");
thumbItemsList = new ArrayList<ThumbnailItems>();
final String userObjectID = ProfileActivity.objectID;
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(userObjectID, new GetCallback<ParseUser>() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null) {
try {
ParseQuery<ParseObject> postQuery = new ParseQuery<ParseObject>(
"Post");
postQuery.whereEqualTo("user", parseUser);
postQuery.orderByDescending("createdAt");
objectList = postQuery.find();
for (ParseObject parseObject : objectList) {
final ThumbnailItems thumbItems = new ThumbnailItems();
ParseFile image = (ParseFile) parseObject.get("image");
if (image != null) {
image.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if (e == null) {
Bitmap userImage = BitmapFactory.decodeByteArray(bytes, 0
, bytes.length);
thumbItems.setThumb(userImage);
} else {
Log.d("ParseException", "Error: " + e.getMessage());
e.printStackTrace();
}
}
});
}
thumbItemsList.add(thumbItems);
}
gridViewAdapter = new GridViewAdapter(getContext()
, thumbItemsList);
gridView.setAdapter(gridViewAdapter);
swipeRefreshLayout.setRefreshing(false);
} catch (ParseException e1) {
e1.printStackTrace();
}
} else {
e.printStackTrace();
Log.i("ParseException", e.getMessage());
}
}
});
}
这是我的适配器:
package co.eshg.limo4.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import co.eshg.limo4.R;
import co.eshg.limo4.data.ThumbnailItems;
public class GridViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
private List<ThumbnailItems> thumbnailsItemsList = null;
private ArrayList<ThumbnailItems> arrayList;
public GridViewAdapter(Context context, List<ThumbnailItems> thumbnailItemsList) {
this.context = context;
this.thumbnailsItemsList = thumbnailItemsList;
inflater = LayoutInflater.from(context);
this.arrayList = new ArrayList<>();
this.arrayList.addAll(thumbnailItemsList);
}
public class ViewHolder {
ImageView imgThumb;
}
@Override
public int getCount() {
return thumbnailsItemsList.size();
}
@Override
public Object getItem(int position) {
return thumbnailsItemsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.profile_viewpager_tab1_children, null);
holder.imgThumb = (ImageView) view.findViewById(R.id.imgThumb);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Bitmap image = thumbnailsItemsList.get(position).getThumb();
holder.imgThumb.setImageBitmap(image);
return view;
}
}
修改
所以我一直试图搞砸它,我已经让它上班了。我现在的问题是,在我点击屏幕之前图像不会加载。我不知道为什么会这样。
解决方法是将thumbItemsList.add(thumbItems)
置于if条件(if (e == null)
)内,我不知道为什么它不起作用。
答案 0 :(得分:0)
看起来不知道为什么会发生这种情况..
但我建议您使用此库here
它使用了LazyLoad
的概念在您的gradle文件中
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
您只需要设置一次(打开项目时执行)
private void ConfigureImageLoader(){
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(true)
.cacheOnDisk(true)
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300))
.build();
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
config.defaultDisplayImageOptions(defaultOptions);
config.memoryCache(new WeakMemoryCache());
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
//config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(10 * 1024 * 1024); // 10 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
ImageLoader.getInstance().init(config.build());
}
您的适配器
private DisplayImageOptions options;
public GridViewAdapter(Context context, List<ThumbnailItems> thumbnailItemsList) {
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.naodisponivel)
.showImageForEmptyUri(R.drawable.naodisponivel)
.showImageOnFail(R.drawable.naodisponivel)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.displayer(new SimpleBitmapDisplayer())
.build();
}
你的getView中的
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
//
} else {
//
}
ImageLoader imageLoader = ImageLoader.getInstance();
final String finalImgURL = "www.mysite.com/myimage.jpg" //change url here
imageLoader.displayImage(finalImgURL, holder.imgThumb, options);
return view;
}
下载LazyLoad中的图像,下次使用celuar中的缓存时不需要再次下载图像,当然您配置选项
我帮了。