1

I've got a problem with too long loading of my activity using ListView with CustomAdapter. I really don't know what exactly could solve this problem. Thank you.

Here is my CustomAdapter code:

   package com.example.simon.gamesshop;

   /**
    * Created by Simon on 26.3.2016.
    */
   import android.content.Context;
   import android.graphics.Bitmap;
   import android.os.AsyncTask;
   import android.util.Log;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.ViewGroup;
   import android.widget.BaseAdapter;
   import android.widget.ImageView;
   import android.widget.TextView;
   import android.widget.Toast;

   import java.util.concurrent.ExecutionException;

   public class CustomAdapter extends BaseAdapter{
       String [] Name;
       String[] ImageURL;
        String[] UID;
       int[] Count;
       Context context;
       private static LayoutInflater inflater=null;
       public CustomAdapter(MainActivity mainActivity, String[] Name, int[]                              Count,String[] ImageURL, String[] UID) {
    // TODO Auto-generated constructor stub
    context=mainActivity;
    this.Name=Name;
    this.ImageURL=ImageURL;
    this.UID = UID;
    this.Count = Count;

    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Name.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView NTV;
    TextView CTV;
    TextView ID;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.row, null);
    holder.NTV=(TextView) rowView.findViewById(R.id.name);
    holder.img=(ImageView) rowView.findViewById(R.id.cover_image);
    holder.CTV=(TextView) rowView.findViewById(R.id.count);
    holder.ID=(TextView) rowView.findViewById(R.id.uid);

    holder.NTV.setText(Name[position]);
    holder.CTV.setText(Integer.toString(Count[position]));
    holder.img.setImageBitmap(getImage(ImageURL[position]));
    holder.ID.setText(UID[position]);
    return rowView;
}


protected Bitmap getImage (String link){
    Bitmap b = null;
    AsyncTask<String, String, Bitmap> img = new Image();
    img.execute(link);
    try {
        return b = (Bitmap) img.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}

}

And here is part of my code from main activity

    public void GetAll() {
    String Json = "";
    ArrayList<Game> GameList = new ArrayList<Game>();
    AsyncTask<String, String, String> con = new Connector();
    con.execute("GETALL");
    try {
        Json = (String) con.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    ListView viewGL = (ListView)findViewById(R.id.viewGL);
    AllListBuilder(Json, GameList);


    String[] Names = new String[GameList.size()];
    int[] Counts = new int[GameList.size()];
    String[] ImageURLs = new String[GameList.size()];
    String[] UIDs = new String[GameList.size()];

    for(int i=0; i<GameList.size() ;i++){
        Names[i] = GameList.get(i).getName();
        Counts[i] = GameList.get(i).getCount();
        ImageURLs[i]= GameList.get(i).getImage();
        String ID = GameList.get(i).getUID();
        System.out.println("Nastavuje hre[" + i + "] ID: " +ID);
        UIDs[i] = ID;
    }

    viewGL.setAdapter(new CustomAdapter(this, Names, Counts, ImageURLs,         UIDs));
}

And this is xml file of one row of ListView item

Simon Štefunko
  • 33
  • 1
  • 10
  • you have URL for getting image?? – Nils Apr 07 '16 at 07:42
  • @Simon : You are inflating a seperate view every time for every single item :o Why dont you make use of convertview for better performance?? Try checking if (convertView == null) { convertView = inflater.inflate(R.layout.row, null); }. Using convertview will offload the burden on the system for inflating views everytime for every item in listView :) – Sandeep Bhandari Apr 07 '16 at 07:46
  • load your image asynchronously so the all data are display well and Image is load slowly and display. –  Apr 07 '16 at 07:49
  • @Simon Štefunko : Unless you have any specific purpose, try using libraries like Glide or Picaso to load the image for you :) They are easy to use, takes care of most of the stuffs and loads the images asynchronously and efficiently and also cache them for future uses :) – Sandeep Bhandari Apr 07 '16 at 07:50

0 Answers0