我对Android开发人员很陌生,我不是那种在没有深入研究的情况下提出问题的人(这实际上是我的第一次),但在这里我被卡住了...
我创建了一个自定义适配器来填充gridview,一切都可以正常使用假数据。但是在清理数据时,存在一个问题。我的日志说它是由" java.lang.UsupportedOperationException"
引起的如果我简化了我的代码,这里有些东西不起作用:
package be.julot.popularmovies;
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.GridView;
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.Arrays;
import java.util.List;
public class PosterGridFragment extends Fragment {
private MoviePosterItemAdapter moviePosterAdapter;
public PosterGridFragment() {}
MoviePosterItem[] moviePosterItems = {
new MoviePosterItem("Test movie", "7.6", R.drawable.test),
new MoviePosterItem("Dude, where is my car?", "2.2", R.drawable.test),
new MoviePosterItem("Yoooo", "7.2", R.drawable.test),
new MoviePosterItem("Ulysse is here", "5.4", R.drawable.test),
new MoviePosterItem("Come on baby", "7.9", R.drawable.test),
new MoviePosterItem("Limit title to 30 chars please", "5.3", R.drawable.test),
new MoviePosterItem("Doesn't he?", "9.1", R.drawable.test),
new MoviePosterItem("Internetar", "2.4", R.drawable.test)
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.poster_grid_fragment, container, false);
moviePosterAdapter = new MoviePosterItemAdapter(getActivity(), Arrays.asList(moviePosterItems));
GridView posterGrid = (GridView) rootView.findViewById(R.id.movie_grid);
posterGrid.setAdapter(moviePosterAdapter);
moviePosterAdapter.clear();
return rootView;
}
moviePosterAdapter.clear()出现问题; 通常,我在其他地方调用clear()方法(在ASyncTask的onPostExecute中),但问题是相同的。
我的自定义适配器扩展了ArrayAdapter,如下所示:
package be.julot.popularmovies;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class MoviePosterItemAdapter extends ArrayAdapter<MoviePosterItem> {
private final String LOG_TAG = MoviePosterItemAdapter.class.getSimpleName();
public MoviePosterItemAdapter(Activity context, List<MoviePosterItem> moviePosterItems) {
super(context, 0, moviePosterItems);
}
@Override
public View getView(int position, View view, ViewGroup parent){
MoviePosterItem moviePosterItem = getItem(position);
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.movies_grid_item, parent, false);
}
ImageView posterImage = (ImageView) view.findViewById(R.id.posterImage);
posterImage.setImageResource(moviePosterItem.moviePoster);
TextView movieTitle = (TextView) view.findViewById(R.id.movie_title);
movieTitle.setText(moviePosterItem.movieTitle);
return view;
}
}
我应该覆盖自定义适配器类中的clear()方法吗?我看过原来的方法,但我不知道该怎么做。
这是我的自定义项目类:
package be.julot.popularmovies;
//Defines each item in the movie poster grid. Each item shows a title, the average of votes and the
//movie poster in background. See movies_grid_item.xml
import java.util.ArrayList;
public class MoviePosterItem {
String movieTitle;
String movieAverageVote;
int moviePoster;
public MoviePosterItem(String mTitle, String mAverageVote, int mPoster) {
this.movieTitle = mTitle;
this.movieAverageVote = mAverageVote;
this.moviePoster = mPoster;
}
}
非常感谢你可能有的任何答案或任何关于如何自己找到它的线索!我看过类似的帖子,但无法找出我的代码中出了什么问题。
答案 0 :(得分:0)
在适用于Frgament的onActivityCreated中填充适配器。
答案 1 :(得分:0)
你的Android代码很好,Java数组操作技术在这里有问题。您将Arrays.asList()
传递给适配器 - 此方法返回一个修复大小数组,因此您无法删除或添加元素。这会一直存储到ArrayAdapter
(mObjects
中),当你打电话给它时,你有一个很好的例外,那就是尝试做一些你不应该做的事情。
所以它是:
moviePosterAdapter = new MoviePosterItemAdapter(getActivity(), new ArrayList<>(Arrays.asList(moviePosterItems)));
或
ArrayList (new ArrayList<>() + add(new MoviePosterItem()) * N)