簡體   English   中英

圖像加載庫畢加索未將圖像加載到imageview中

[英]Image Loading Library Picasso not loading image into imageview

我正在嘗試使用通過使用Picasso Library獲取數據的圖像適配器使用網格視圖創建的帶有圖庫的圖像庫來創建android應用,但未顯示任何圖像。這可能是上下文問題或在Picasso庫中,但我不確定問題出在哪里因為我沒有錯誤。根據日志,一切正常

**package com.example.android.popmovies;

import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * A placeholder fragment containing a simple view.
 */
public class PopMovieFragment extends Fragment {
    ImageAdapter imageAdapter;
    public PopMovieFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View view = inflater.inflate(R.layout.fragment_main,container,false);

        GridView gridView = (GridView) view.findViewById(R.id.gridview);

        imageAdapter    = new ImageAdapter(getActivity(),new ArrayList<String>());

        gridView.setAdapter(imageAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("check","ok");
            }
        });

        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void onStart() {
        super.onStart();
        updateMovies();
    }

    public void updateMovies() {
        FetchMovieTask movieTask = new FetchMovieTask();
        movieTask.execute();
    }

    public class FetchMovieTask extends AsyncTask<String, Void, String[]> {


        final String LOG_TAG = FetchMovieTask.class.getSimpleName();

        private String[] getMovieImageFromJson(String movieJsonStr)
                throws JSONException {

            ArrayList<String> movieResults = new ArrayList<>();
            String posterUrl;
            final String OWM_MOVIE_RESULTS = "results";
            JSONObject jsonObject = new JSONObject(movieJsonStr);
            if (jsonObject.has(OWM_MOVIE_RESULTS)) {
                JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);

                //JSONArray movieArray = jsonObject.getJSONArray(OWM_MOVIE_RESULTS);

                for (int i = 0; i < movieArray.length(); i++) {
                    JSONObject movieObject = movieArray.getJSONObject(i);
                    posterUrl = movieObject.getString("poster_path");
                    movieResults.add(posterUrl);
                    Log.v(LOG_TAG, posterUrl);
                }
                for (String items : movieResults){
                    Log.v(LOG_TAG,items);
                }



                // return null;
            }

            return movieResults.toArray(new String[0]);



            // Log.v(LOG_TAG,posterUrl);


        }


        @Override
        protected String[] doInBackground(String... params) {


            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;


            String movieJsonStr = null;
            //parameters for api link will be here

            try {

                final String MOVIE_BASE_URL = "https://api.themoviedb.org/3/movie";
                final String APPID_PARAM = "api_key";
                final String CRITERIA_FOR_MOVIE_SELECTION_TOP = "top_rated";
                final String CRITERIA_FOR_MOVIE_SELECTION_POPULAR = "popular";



                Uri builtUri = Uri.parse(MOVIE_BASE_URL).buildUpon()
                        .appendPath(CRITERIA_FOR_MOVIE_SELECTION_TOP)
                        .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_MOVIEDB_API_KEY)
                        .build();

                Log.v("TAG","BuiltURI" + builtUri.toString());

                URL url = new URL(builtUri.toString());

                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read input stream into a String

                InputStream inputstream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputstream == null) {

                    movieJsonStr = null;
                }
                reader = new BufferedReader(new InputStreamReader(inputstream));

                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    movieJsonStr = null;
                }

                movieJsonStr = buffer.toString();

                Log.v(LOG_TAG, "Movie Json String" + movieJsonStr);


            } catch (IOException e) {
                Log.e(LOG_TAG, "error here" + e);

                movieJsonStr = null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "ErrorClosingStream", e);
                    }
                }


            }

            try {
                // here will be method call for JSon parsing.
                 String [] check_items = getMovieImageFromJson(movieJsonStr);

                for (String items : check_items){
                    Log.v(LOG_TAG,items+"new");
                }
                return  check_items;

            } catch (JSONException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(String[] result) {

            if (result != null) {


                for (String resultItems : result){
                    Log.v(LOG_TAG,resultItems+"GoodLuck");
                    final String PICTURE_BASE_URL = "http://image.tmdb.org/t/p/w185/";
                    final String PICTURE_URL_END = resultItems;
                    Uri builtUri = Uri.parse(PICTURE_BASE_URL).buildUpon()
                            .appendPath(PICTURE_URL_END.replace("/",""))
                            .build();
                    Log.v(LOG_TAG,resultItems+"GoodLuck"+builtUri.toString());
                    imageAdapter.setmresultItems(builtUri.toString());
                }
            }
        }

    }
}



class ImageAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<String> mresultItems;




        public ImageAdapter(Context c,ArrayList<String> resultItems){
            mContext = c;
            mresultItems = resultItems;

        }

        public int getCount(){
           // return mThumbsIds.length;
            return  0;
        }

        public Object getItem(int position){
            return  null;
        }

        public long getItemId(int position){
            return  0;
        }


    public void setmresultItems(String resultItems){
                mresultItems.add(resultItems);


    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;

            if (convertView == null){
//            if it is not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);

            }else{
                imageView = (ImageView) convertView;
            }
            //imageView.setImageResource(mThumbsIds[position]);
            for(String picResultUrl : mresultItems){
                Picasso.with(mContext).load(picResultUrl).into(imageView);


            }


            return  imageView;
        }



        //references to our Images

        //private Integer[] mThumbsIds ;





}**

getView()方法中不應包含for循環,它已被列表中的每個項目調用。

確保返回正確的計數:

    public int getCount(){
        return mresultItems.size();
    }

然后,僅在getView()中使用數據源的當前位置:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if (convertView == null){
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85,85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);

        }else{
            imageView = (ImageView) convertView;
        }

        //modified:
        String picUrl = mresultItems.get(position);
        Picasso.with(mContext).load(picUrl).into(imageView);
        }

        return  imageView;
    }

另外,在更新適配器的數據源之后,請在notifyDataSetChanged()調用適配器上的onPostExecute()

    @Override
    protected void onPostExecute(String[] result) {

        if (result != null) {
            for (String resultItems : result){
                Log.v(LOG_TAG,resultItems+"GoodLuck");
                final String PICTURE_BASE_URL = "http://image.tmdb.org/t/p/w185/";
                final String PICTURE_URL_END = resultItems;
                Uri builtUri = Uri.parse(PICTURE_BASE_URL).buildUpon()
                        .appendPath(PICTURE_URL_END.replace("/",""))
                        .build();
                Log.v(LOG_TAG,resultItems+"GoodLuck"+builtUri.toString());
                imageAdapter.setmresultItems(builtUri.toString());
            }
            //Add this here:
            imageAdapter.notifyDataSetChanged();
        }
    }

我發現我沒有在onCreate方法中返回視圖,這是黑屏的根本原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM