将图像从Parse.com加载到RecyclerViewAdapter

时间:2016-02-10 10:59:10

标签: android parse-platform android-recyclerview

我正在尝试将图像加载到RecyclerView Adapater中我遇到错误

if(photo!= null) - 意外令牌。

返回newPhotoFilterViewHolder(view); - 无效声明返回类型必需。

公共类PhotoFiltersAdapter扩展了RecyclerView.Adapter - 声明抽象或创建抽象方法。

这是我将代码加载到RecyclerViewAdapter

的实现代码
public class PhotoFiltersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private int itemsCount = 12;
    ImageView image;

    // Fields for the map radius in feet
    private float radius = InstaMaterialApplication.getSearchDistance();
    /**
     * Helper class to get the user location.
     */
    private GeoLocationHelper geoLocationHelper = new GeoLocationHelper();

    // Adapter for the Parse query
    private ParseQueryAdapter<FilterImages> postsQueryAdapter;

    public PhotoFiltersAdapter(Context context) {
        this.context = context;
    }

    @Override
    public  RecyclerView.ViewHolder onCreateViewHolder(final FilterImages post, ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(context).inflate(R.layout.item_photo_filter, parent, false);
        // Set up the query adapter
        postsQueryAdapter = new ParseQueryAdapter<FilterImages>(this, factory) {
            ParseFile photo = (ParseFile) post.get("image");
            if(photo!=null)

            {
                photo.getDataInBackground(new GetDataCallback() {

                    @Override
                    public void done(byte[] data, ParseException e) {
                        // TODO Auto-generated method stub
                        if (data != null && e == null) {
                            Bitmap bitmap = BitmapFactory
                                    .decodeByteArray(data, 0,
                                            data.length);
                            image.setImageBitmap(bitmap);

                        } else {
                            //ParseException e
                        }
                    }
                });
            }

            else

            {
                //picture_not_available
            }

            TextView contentView = (TextView) view.findViewById(R.id.content_view);
            ImageView image=(ImageView)view.findViewById(R.id.picture_view);
            TextView usernameView = (TextView) view.findViewById(R.id.username_view);
            contentView.setText(post.getText());

            return newPhotoFilterViewHolder(view);
        };
    }




    private ParseGeoPoint geoPointFromLocation(Location loc) {
        return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude());
    }

    // Set up a customized query
    ParseQueryAdapter.QueryFactory<FilterImages> factory =
            new ParseQueryAdapter.QueryFactory<FilterImages>() {
                public ParseQuery<FilterImages> create() {
                    Location myLoc = geoLocationHelper.getCurrentLocation();
                    ParseQuery query = FilterImages.getQuery();
                    query.include("PlaceName");
                    query.orderByDescending("AreaFilters");
                    query.whereWithinKilometers("AreaFilters", geoPointFromLocation(myLoc), radius);
                    query.setLimit(itemsCount);
                    return query;
                }
            };



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    }

    @Override
    public int getItemCount() {
        return itemsCount;
    }

    public static class PhotoFilterViewHolder extends RecyclerView.ViewHolder {

        public PhotoFilterViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我使用glide从Url获​​取图像,在ParseFile对象中有方法.getUrl()

        ParseFile photo = (ParseFile) post.get("image");
        Glide.with(getActivity()).load(photo.getUrl()).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(img_userProfilePicture_bg);