gpt4 book ai didi

java - 难以使用自定义适配器填充 GridView

转载 作者:太空狗 更新时间:2023-10-29 16:14:13 26 4
gpt4 key购买 nike

我在尝试通过覆盖 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 将按预期工作

关于java - 难以使用自定义适配器填充 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com