@Override public void onResponse(final Response response) throws IOException {//okhttp onresponse method try { String json = response.body().string();//get data from cinemalytics Log.v(TAG, json);// if (response.isSuccessful()) { mCurrentmovie = getDATA(json); //mCurrentmovie is an object of Currentmovie getter and setter class. runOnUiThread(new Runnable() { @Override public void run() { String T = mCurrentmovie.getTitle(); tv.setText(T); //successfully show in single textview but can't in listview } }); } else { } } catch (Exception e) { } } });
//返回json对象
private Currentmovie getDATA(String json) throws JSONException { JSONArray values = new JSONArray(json); // JSONArray values = object.getJSONArray("0"); // JSONObject ob = object.getJSONObject("0"); JSONObject obj = values.getJSONObject(0); String Title = obj.getString("Title"); String imgg = obj.getString("PosterPath"); Currentmovie c = new Currentmovie(); c.setImageView(imgg); Log.e(TAG, Title); c.setTitle(Title); return c; }
//我希望listview图片中的此数据也包含在data.please中,请逐步说明//来自Cinemalytics的json数据
[ { "Id": "7582268f", "ImdbId": "tt0979913", "OriginalTitle": "Life Mein Kabhie Kabhiee", "Title": "Life Mein Kabhie Kabhiee", "Description": "", "TrailerLink": "", "TrailerEmbedCode": "", "Country": "IN", "Region": "BOLLYWOOD", "Genre": "", "RatingCount": 1, "Rating": 2.0, "CensorRating": "", "ReleaseDate": "1/1/1900", "Runtime": 0, "Budget": 0, "Revenue": 0, "PosterPath": "https://s3-ap-southeast-1.amazonaws.com/cinemalytics/movie/70C66638BF359AEA9E9FEE83AE46D18C.jpg" }
]
// movieAdapter类//在getSystemService中显示错误,其余所有代码都可以…。
package com.example.maau.movieinfo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class MovieAdapter extends BaseAdapter { private List<Currentmovie> movieData; private static LayoutInflater inflater = null; public MovieAdapter(List<Currentmovie> movieData) { this.movieData = movieData; inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return movieData.size(); } @Override public Object getItem(int position) { return movieData.get(position); } @Override public long getItemId(int position) { return position; } public static class ViewHolder{ public TextView movieTitle; public ImageView movieImage; } public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if(convertView==null){ vi = inflater.inflate(R.layout.row, null); holder = new ViewHolder(); holder.movieTitle = (TextView) vi.findViewById(R.id.tv); // holder.movieImage=(ImageView)vi.findViewById(R.id.image); vi.setTag( holder ); } else holder=(ViewHolder)vi.getTag(); holder.movieTitle.setText(movieData.get(position).getTitle()); return vi; } }
得到响应后,请遵循以下代码,
首先在所有方法的顶部上方创建以下2个元素。
List<CurrentMovie> moviesList = new ArrayList<>(); MovieAdapter mAdapter; // Now on response change code as below if (response.isSuccessful()) { manageJson(json); } public void manageJson(String result) { try { movieList = new ArrayList<>(); JsonArray jsonArray = new JSONArray(result); for(int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJsonObject(i); String Title = obj.getString("Title"); String imgg = obj.getString("PosterPath"); Currentmovie c = new Currentmovie(); c.setImageView(imgg); c.setTitle(Title); movieList.add(c); } mAdapter = new MovieAdapter(YourActivityName.this, movieList); listViewObject.setAdapter(mAdapter); } catch(Exception e) { System.out.println("Error in Result as " + e.toString()); } }
现在,使用一个适配器在ListView中设置数据
public class MovieAdapter extends BaseAdapter { private final Context context; private List<CurrentMovie> movieData; private static LayoutInflater inflater=null; public MovieAdapter(Context context, List<CurrentMovie> movieData) { this.context = context; this.movieData = movieData; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return movieData.size(); } @Override public Object getItem(int position) { return movieData.get(position); } @Override public long getItemId(int position) { return position; } public static class ViewHolder{ public TextView movieTitle; public ImageView movieImage; } public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if(convertView==null){ vi = inflater.inflate(R.layout.tabitem, null); holder = new ViewHolder(); holder.movieTitle = (TextView) vi.findViewById(R.id.title); holder.movieImage=(ImageView)vi.findViewById(R.id.image); vi.setTag( holder ); } else holder=(ViewHolder)vi.getTag(); holder.movieTitle.setText(movieData.get(position).getTitle()); return vi; } }