0

Well,I was following android hive.I tried using my own code.But in CustomAdapter,I hit a rock which causes not to display images.Below files are there,and this is the json.I know i messed up with CustomAdapter,Please do suggest.Thanks

Structure:

AppController

LruBitmapCache

MainActicity

MovieCustomAdapter

//I 'll be uploading only MovieCustomAdapter and MainActivity.java because rest LruBitmap and AppCOntroller is same.

MovieCustomAdapter.java

public class MovieCustomAdapter extends BaseAdapter {


static class ViewHolder{
    private NetworkImageView networkImageView;
    private TextView title_1,review_2,genre_3,year_4;
}

private static LayoutInflater layoutInflater=null;
private Context context;
String title[],review[],year[];
String image[];
ViewHolder viewHolder;
ImageLoader imageLoader= AppController.getInstance().getImageLoader();

public MovieCustomAdapter(Context context,String image[],String title[],String review[],String year[]){
    this.context=context;
    this.image=image;
    this.title=title;
    this.review=review;
    //this.genre=genre;
    this.year=year;
}
@Override
public int getCount() {
    return title.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;
    if(v==null){
        layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=layoutInflater.inflate(R.layout.movie_custom_layout,parent,false);

        viewHolder=new ViewHolder();
        viewHolder.networkImageView=(NetworkImageView)v.findViewById(R.id.thumbnail);
        viewHolder.title_1=(TextView)v.findViewById(R.id.title);
        viewHolder.review_2=(TextView)v.findViewById(R.id.rating);
        viewHolder.genre_3=(TextView)v.findViewById(R.id.genre);
        viewHolder.year_4=(TextView)v.findViewById(R.id.releaseYear);
        v.setTag(viewHolder);
    }else {
        viewHolder=(ViewHolder)v.getTag();
    }
    viewHolder.title_1.setText(title[position]);
    viewHolder.review_2.setText(review[position]);
    viewHolder.year_4.setText(year[position]);

 //HERE..................viewHolder.networkImageView  whatever !

    return v;
}

}

//This is where my doubts are there for showing Images.This is my way to use custom adapter.Before that I was using my local images to show this way,but since these images are getting received and will be displaying in List view,so here the confusion.In above-mentioned,tutorial they followed another approach but please try to stick with my way.

MainActivity.java

public class MainActivity extends AppCompatActivity {
private MovieCustomAdapter movieCustomAdapter;
private ListView lv1;

String title_k[],review_k[],year_k[];
String image_k[];
ProgressDialog progressDialog;
private static String url="http://api.androidhive.info/json/movies.json";
ArrayList<HashMap<String,String>> hashMapArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    lv1=(ListView)findViewById(R.id.listView);

    progressDialog=new ProgressDialog(this);
    progressDialog.setTitle("Loading");
    progressDialog.setMessage("getting Images");
    progressDialog.setIcon(R.mipmap.ic_launcher);
    progressDialog.setCancelable(false);

    showDialog();

    JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response_jsonArray) {
            Log.d("Rcv",response_jsonArray.toString());


            hashMapArrayList=new ArrayList<HashMap<String, String>>();

            for (int i=0;i<response_jsonArray.length();i++){
                try {
                    JSONObject jsonObject=response_jsonArray.getJSONObject(i);

                    HashMap<String,String> hashMap=new HashMap<>();
                    hashMap.put("image_key",jsonObject.getString("image"));
                    hashMap.put("title_key",jsonObject.getString("title"));
                    hashMap.put("review_key",jsonObject.getString("rating"));
                    hashMap.put("year_key",jsonObject.getString("releaseYear"));


                    hashMapArrayList.add(hashMap);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            image_k=new String[hashMapArrayList.size()];
            title_k=new String[hashMapArrayList.size()];
            review_k=new String[hashMapArrayList.size()];
            year_k=new String[hashMapArrayList.size()];



            for(int i=0;i<hashMapArrayList.size();i++) {

                image_k[i]=hashMapArrayList.get(i).get("image_key");
                title_k[i]=hashMapArrayList.get(i).get("title_key");
                review_k[i]=hashMapArrayList.get(i).get("review_key");
                year_k[i]=hashMapArrayList.get(i).get("year_key");


                movieCustomAdapter=new MovieCustomAdapter(MainActivity.this,image_k,title_k,review_k,year_k);
                lv1.setAdapter(movieCustomAdapter);
                Log.d("Log","myACustom");
            }


            movieCustomAdapter.notifyDataSetChanged();
            hideDialog();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(getApplicationContext(), "server error"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            hideDialog();

        }
    });
    AppController.getInstance().addToRequestQueue(jsonArrayRequest);



}
private void showDialog(){
    if(!progressDialog.isShowing()){
        progressDialog.show();
    }
}
private void hideDialog(){
    if(progressDialog.isShowing()){
        progressDialog.dismiss();
    }

}
}

Deeply Appreciated

Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
hemen
  • 1,460
  • 2
  • 16
  • 35

0 Answers0