getView()方法用于在使用Picasso库解析后打印json响应没有被执行。当我调试我的代码时,它会跳过getView()方法,输出为空白屏幕。
package com.example.saurabharora.movies;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
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.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 MainActivityFragment extends Fragment {
String jsonstr;
private ArrayAdapter<String> PostsAdapter;
ArrayList posters = new ArrayList();
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridView = (GridView) rootView.findViewById(R.id.image);
gridView.setAdapter(new PostsAdapter(getActivity(), posters));
return rootView;
}
private void updateMovies() {
FetchMovies MovieTask = new FetchMovies();
MovieTask.execute();
}
@Override
public void onStart() {
super.onStart();
updateMovies();
}
public class FetchMovies extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String jsonstr = null;
try {
URL url = new URL("http://api.themoviedb.org/3/movie/top_rated?api_key=OPEN_MOVIES_API_KEY");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
jsonstr = buffer.toString();
//TextView response = (TextView) findViewById(R.id.list);
//response.setText(jsonstr);
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
ArrayList posters = new ArrayList();
try {
JSONObject response = new JSONObject(jsonstr);
JSONArray results = response.optJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject movie = results.getJSONObject(i);
posters.add(movie.optString("poster_path"));
}
Log.v("posterpath", posters.toString());
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
return null;
}
protected void onPostExecute(String[] result) {
if (result == null) {
System.out.println("Error");
}
if (result != null) {
PostsAdapter.clear();
for (String MoviesStr : result) {
PostsAdapter.add(MoviesStr);
}
// New data is back from the server. Hooray!
}
}
}
class PostsAdapter extends ArrayAdapter {
Activity context;
ArrayList posters = null;
public PostsAdapter(Activity context, ArrayList posters) {
super(context, R.layout.list, posters);
this.context = context;
this.posters = posters;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = context.getLayoutInflater().inflate(R.layout.list, parent, false);
ImageView loadimages = (ImageView) convertView.findViewById(R.id.url);
Picasso.with(context).load("http://image.tmdb.org/t/p/w185/" + posters.get(position)).into(loadimages);
return convertView;
}
}
}
答案 0 :(得分:0)
我认为您错过了@Override
方法的getView()
注释:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = context.getLayoutInflater().inflate(R.layout.list, parent, false);
ImageView loadimages = (ImageView) convertView.findViewById(R.id.url);
Picasso.with(context).load("http://image.tmdb.org/t/p/w185/" + posters.get(position)).into(loadimages);
return convertView;
}
希望这会有所帮助:)
答案 1 :(得分:0)
在代码中进行以下更改:
在onCreateView中: -
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridView = (GridView) rootView.findViewById(R.id.image);
mPostsAdapter = new PostsAdapter(getActivity(), posters);
gridView.setAdapter(mPostsAdapter);
return rootView;
}
在AsyncTask中的onPostExecute中: -
protected void onPostExecute(String[] result) {
if (result == null) {
System.out.println("Error");
}
if (result != null) {
mPostsAdapter.clear();
for (String MoviesStr : result) {
mPostsAdapter.add(MoviesStr);
}
// New data is back from the server. Hooray!
}
}