Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

java - how to show in listview??(using okhttp library)

                @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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After getting response follow below code,

First create below 2 elements at top out side of all methods.

    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 obj = 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());
   }
}

Now Take one Adapter to set Data in 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;
     }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...