GridView仅在单击时加载项目

时间:2016-04-18 17:21:32

标签: android parse-platform android-gridview

所以我正在尝试创建一个GridView,它从运行Parse API的服务器加载图像。

我遇到的问题是当我将图像从服务器加载到GridView中时,我必须单击片段将它们加载到屏幕上。

我不知道为什么会这样。

这是我的AsyncTask:

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        thumbItemsList = new ArrayList<PostItems>();
        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 (final ParseObject parseObject : objectList) {
                            thumbItems = new PostItems();
                            String first_name = parseObject.getString("first_name");
                            String last_name = parseObject.getString("last_name");
                            thumbItems.setName(first_name + " " + last_name);
                            ParseFile image = (ParseFile) parseObject.get("image");
                            image.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] bytes, ParseException e) {
                                    if (e == null) {
                                        Bitmap userImage = BitmapFactory.decodeByteArray(bytes, 0
                                                , bytes.length);
                                        thumbItems.setImage(userImage);
                                    } else {
                                        Log.d("ParseException", "Error: " + e.getMessage());
                                        e.printStackTrace();
                                    }
                                }
                            });
                            thumbItemsList.add(thumbItems);
                        }
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                } else {
                    e.printStackTrace();
                    Log.i("ParseException", e.getMessage());
                }
            }
        });

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        gridViewAdapter = new GridViewAdapter(getContext(), thumbItemsList);
        gridView.setAdapter(gridViewAdapter);
        swipeRefreshLayout.setRefreshing(false);
    }
}

这是我的GridView适配器:

package co.eshg.limo4.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import co.eshg.limo4.PostActivity;
import co.eshg.limo4.R;
import co.eshg.limo4.data.PostItems;
import co.eshg.limo4.data.GridViewItem;
import co.eshg.limo4.data.ThumbnailItems;

public class GridViewAdapter extends BaseAdapter implements View.OnClickListener {
Context context;
LayoutInflater inflater;
private List<PostItems> thumbnailsItemsList = null;
private ArrayList<PostItems> arrayList;

public GridViewAdapter(Context context, List<PostItems> thumbnailItemsList) {
    this.context = context;
    this.thumbnailsItemsList = thumbnailItemsList;
    inflater = LayoutInflater.from(context);
    this.arrayList = new ArrayList<>();
    this.arrayList.addAll(thumbnailItemsList);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imgThumb:

            break;
    }
}

public class ViewHolder {
    GridViewItem gridViewItem;
}

@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(final 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.gridViewItem = (GridViewItem) view.findViewById(R.id.imgThumb);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    final Bitmap image = thumbnailsItemsList.get(position).getImage();
    holder.gridViewItem.setImageBitmap(image);

    holder.gridViewItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, PostActivity.class);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            intent.putExtra("PostImage", byteArray);
            intent.putExtra("FullName", thumbnailsItemsList.get(position).getName());
            context.startActivity(intent, null);
        }
    });

    return view;
}
}

到目前为止我尝试过的是从我的drawable文件夹中加载本地图像而不是从服务器加载图像,这非常有效。我已经通过使用final Bitmap image = thumbnailsItemsList.get(position).getImage();

替换适配器final Bitmap image = BitmapFactory.decodeResource(context.getResources(), R.drawable.dairy_back);中的这一行来完成此操作

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

  

image.getDataInBackground

这行代码将从服务器获取数据。这需要一段时间。但是你把它放在Thread上。因此,在从服务器获取位图之前,它将执行此 thumbItemsList.add(thumbItems)。这是因为gridview不会加载图像,因为数组列表仍然没有位图值,但您已经将它添加到适配器上。

你应该添加thumbItems.setUrl(stringUrlImages)并将 image.getDataInBackground 移动到getView方法