@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) {
}
}
});
//return json object
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;
}
//i want this data in listview image also included in data.please tell step by step
//json data from cinemalytics
[
{
"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 class
//shows error in getSystemService,rest of all code is ok....
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;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…