簡體   English   中英

使用自定義適配器填充GridView的困難

[英]Difficulty in populating the GridView using a Custom Adapter

我在嘗試通過覆蓋onPostExecute方法填充GridView時遇到問題。 我強制關閉adapter.add(oneMovie); 在onPostExecute方法中。

我的代碼鏈接如下:

MainActivityFragment.java

public class MainActivityFragment extends Fragment {
        MovieAdapter adapter;

   public MainActivityFragment() {

    }

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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater Inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        Inflater.inflate(R.menu.moviefragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            MovieDetails movie = new MovieDetails();
            movie.execute();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    // Will contain the raw JSON response as a string.
    String movieinfo = null;
    private final String LOG_TAG = MovieDetails.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        SingleMovie[] movieList = {};
        adapter = new MovieAdapter(getActivity(), Arrays.asList(movieList));
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        GridView gridview = (GridView) rootView.findViewById(R.id.gridView);
        gridview.setAdapter(adapter);
        return rootView;
    }

    public class MovieDetails extends AsyncTask<Void, Void, SingleMovie[]> {   //Line number 84 according to log
        @Override  
        protected void onPostExecute(SingleMovie[] singleMovie) {
            if (singleMovie != null) {
                adapter.clear();
                for (int i = 0; i < singleMovie.length; i++) {
                    SingleMovie oneMovie = singleMovie[i];
                    Log.v(LOG_TAG, oneMovie.movieTitle + oneMovie.movieImage);
                    adapter.add(oneMovie); //Line number 92 according to log
                }
            }
            super.onPostExecute(singleMovie);
        }

        private SingleMovie[] getmovieData(String movieInfo)
                throws JSONException {
            final String MDB_RESULT = "results";
            final String MDB_TITLE = "title";
            final String MDB_POSTER = "poster_path";
            JSONObject moviejson = new JSONObject(movieInfo);
            JSONArray movieArray = moviejson.getJSONArray(MDB_RESULT);
            String baseURL = "http://image.tmdb.org/t/p/w185/";
            SingleMovie[] movieDetails = new SingleMovie[5];
            for (int i = 0; i < 5; i++) {
                JSONObject currentMovie = movieArray.getJSONObject(i);
                String movietitle = currentMovie.getString(MDB_TITLE);
                String moviePosterendURL = currentMovie.getString(MDB_POSTER);
                String moviePosterURL = baseURL + moviePosterendURL;
                movieDetails[i] = new SingleMovie(moviePosterURL, movietitle);
            }
            return movieDetails;
        }

        @Override
        protected SingleMovie[] doInBackground(Void... params) {
            try {

                URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=APPID");
                String movieDbUrl = url.toString();
                Log.v(LOG_TAG, movieDbUrl);
                // Create the request to OpenWeatherMap, and open the connection
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuilder buffer = new StringBuilder();
                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                    // But it does make debugging a *lot* easier if you print out the completed
                    // buffer for debugging.
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                movieinfo = buffer.toString();
                Log.v(LOG_TAG, movieinfo);
            } catch (IOException e) {
                Log.e("PlaceholderFragment", "Error ", e);
                // If the code didn't successfully get the weather data, there's no point in attemping
                // to parse it.
                return null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e("PlaceholderFragment", "Error closing stream", e);
                    }
                }
            }
            try {
                return getmovieData(movieinfo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    }

MovieAdapter.java

public class MovieAdapter extends ArrayAdapter<SingleMovie>{
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SingleMovie singleMoviecontent = getItem(position);
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
        ImageView poster = (ImageView) rootView.findViewById(R.id.movie_poster_image);
        Picasso.with(getContext()).load(singleMoviecontent.movieImage).into(poster);
        TextView name = (TextView) rootView.findViewById(R.id.movie_name);
        name.setText(singleMoviecontent.movieTitle);
        return rootView;
    }

    public MovieAdapter(FragmentActivity context, List<SingleMovie> resource) {
        super(context, 0, resource);
    }

}

SingleMovie.java

public class SingleMovie {
    String movieImage;
    String movieTitle;
    public SingleMovie(String image, String title){
        this.movieImage = image;
        this.movieTitle = title;
    }
}

該代碼的另一部分工作正常。 我已經在各個部分記錄了該日志,並且日志消息與預期的一樣。 代碼在adapter.add(oneMovie);處中斷。 內部postexecute方法。 任何幫助都會很棒。

謝謝

日志消息:

01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: FATAL EXCEPTION: main
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: Process: io.github.the_dagger.movies, PID: 1870
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: java.lang.UnsupportedOperationException
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at java.util.AbstractList.add(AbstractList.java:404)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at java.util.AbstractList.add(AbstractList.java:425)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:92)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:84)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.os.AsyncTask.finish(AsyncTask.java:636)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.os.AsyncTask.access$500(AsyncTask.java:177)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:139)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5308)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)

Arrays.asList返回由數組支持的固定大小的ArrayList ,並且不支持添加或刪除之類的操作。 更改

adapter = new MovieAdapter(getActivity(), Arrays.asList(movieList));

adapter = new MovieAdapter(getActivity(), new ArrayList<SingleMovie>());

add將按預期工作

暫無
暫無

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

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