当前位置:  开发笔记 > 编程语言 > 正文

使用自定义适配器填充GridView的难度

如何解决《使用自定义适配器填充GridView的难度》经验,为你挑选了1个好方法。

当我试图通过覆盖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 {   //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{
    @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 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)

Blackbelt.. 6

Arrays.asList返回ArrayList 由数组支持的固定大小,不支持添加或删除等操作.更改

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

adapter = new MovieAdapter(getActivity(), new ArrayList());

并将add按预期工作



1> Blackbelt..:

Arrays.asList返回ArrayList 由数组支持的固定大小,不支持添加或删除等操作.更改

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

adapter = new MovieAdapter(getActivity(), new ArrayList());

并将add按预期工作

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有