Main Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie);
lv =(ListView)findViewById(R.id.lstMovieData);
moviename.clear();
// tv = (TextView) findViewById(R.id.tv);
Bundle b = getIntent().getExtras();
try {
Title = b.getString("MOVIE");
t = replace(Title);
} catch (Exception e) {
}
String API = "https://api.cinemalytics.com/v1/movie/title/?value=" + t + "&auth_token=<token>";
Toast.makeText(getApplicationContext(), Title, Toast.LENGTH_LONG).show();
OkHttpClient Client = new OkHttpClient();
Request request = new Request.Builder()
.url(API).build();
Call call = Client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(final Response response) throws IOException {
try {
String json = response.body().string();
Log.v(TAG, json);
if (response.isSuccessful()) {
getDATA(json);
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter = new MovieAdapter(getApplicationContext(),moviename);
lv.setAdapter(mAdapter);
}
});
} else {
}
} catch (Exception e) {
}
}
});
}
public String replace(String str) {
return str.replaceAll(" ", "%20");
}
private void getDATA(String json) throws JSONException {
try {
moviename = new ArrayList<>();
Currentmovie c = new Currentmovie();
String story = "About The Story";
JSONArray values = new JSONArray(json);
for(int i = 0; i < values.length(); i++) {
JSONObject jsonObject = values.getJSONObject(i);
String movieTitle = jsonObject.getString("Title");
String disc = jsonObject.getString("Description");
Log.e(TAG,"GIRISH"+movieTitle);
c= new Currentmovie();
c.setTitle("Movie Name::"+movieTitle);
c.setDesc(story+"::
"+disc);
if(jsonObject.getString("Description")==null)
{
c.setDesc(story+"::Not Available");
}
moviename.add(c);
}
}
catch (Exception e)
{
System.out.println("Error in Result as " + e.toString());
}
}
2.MovieAdapter.java
public class MovieAdapter extends BaseAdapter {
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,movieDesc;
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.movieDesc=(TextView)vi.findViewById(R.id.tv1);
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
holder.movieTitle.setText(movieData.get(position).getTitle());
holder.movieDesc.setText(movieData.get(position).getDesc());
return vi;
}
}
//i can successfully show all data except image
//image link comes with "posterpath" key
//tell me how to load image in viewholder of MovieAdapter
//currentmovie is just a getter and setter class
3.Currentmovie.java
public class Currentmovie {
private String mTitle;
private String Description;
public String getDesc() {
return Description;
}
public void setDesc(String desc) {
Description = desc;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…