1

Tried using the following:

Populate Listview from JSON

To make a listview which uses a JsonArray containing Json Objects. For some reason, the 'public View getView(int position, View convertView, ViewGroup parent)' code is fired more times than there are contents in the jsonarray.

I made a control test to check up on this and I found that even with just 1 Jsonobject within the jsonarray, I came up with 32 times the getView code was activated.

I am rather confused as to why this is happening, as my friends have managed to make similar codes to mine, but without the huge number of activations I am suffering from. Am I being rather slow, and this is because the individual Jsonobject has, not only the image and text in them, but about 15 other items within it? Or is ther another cause?

I would appreciate any aid towards this, I am posting the adapter code below:

    public class ArticleAdapter extends BaseAdapter{

    private JSONArray items;
    private Context cont;
    public ArticleAdapter(Context context, JSONArray array)
    {
        super();
        this.items = array;
        this.cont = context;
    }

    @Override
    public int getCount() {
        return items.length();
    }

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

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

    }@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        WebIView sath;
        TextView sati;
        Log.i("Seiji", "Checking! " + position);
        try
        {       
            if(!items.isNull(position))
            {
                JSONObject item = items.getJSONObject(position);
                if (v == null) {
                    v = LayoutInflater.from(cont).inflate(R.layout.saved_articles_listitem, null);
                }           
                sath = (WebIView) v.findViewById(R.id.sathumbnail);
                sati = (TextView) v.findViewById(R.id.satitle);

                if(item.has("image") && sath != null)
                {
                    JSONObject thisImage = item.getJSONObject("image");
                    sath.reset();
                    sath.setImageUrl(thisImage.getString("thumbnail"));
                    sath.loadImage();
                }
                if(sati != null)
                {
                    sati.setText(item.getString("title"));
                }
            }else{
                return null;
            }
        }
        catch(Exception e)
        {
            Log.e("num", "Saved Art Error! " + e.toString());
        }
        return v;
    }
}

the code which activates this class is the following:

ListView savedArtList = (ListView) sav.findViewById(R.id.savelist);
ArticleAdapter savedadapter = new ArticleAdapter(cont, flip);
ArtList.setAdapter(savedadapter);

EDIT:

Thanks to some very helpful advice I was able to figure out what was going wrong. The Listview was resizing itself every time a new row was added because I had set the views height to be 'wrap_content'. I hadnt realised that this would cause problems, but once I had set it to 'fill_parent' (or a set value in other cases), the issue disappeared and I didnt have this problem any more. Thank you againfor the helpful advice!

Community
  • 1
  • 1
SKato
  • 95
  • 6
  • 15

1 Answers1

1

getView will be called many times - per visible cell when the list view is being laid out, per visible cell when the list view is being drawn + more. This is normal behaviour and getView should be efficient. Its possible your images and/or text are making the height of each cell change as they're loaded in, meaning other cells may become visible / go off screen etc.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • thanks for the message. My main concerned though is that when I have many rows, getView is called often, so it is worrying as the images are cached every time it tries to call. So I end up with the program slowing down due to the number of times the code is called. – SKato Nov 16 '11 at 11:02
  • I don't know how WebIView handles the loadImage, but you probably want an in memory cache & a persistent cache (sdcard). That way, if the url has just been downloaded (or retrieved from persistent cache) it can just be set, if it isn't in the memory cache you can look to see if it is in the persistent cache and load it from there if it exists (probably quicker than downloading & saves bandwidth). If you have to download it, save it to the persistent cache at the same time. – FunkTheMonk Nov 16 '11 at 11:20
  • Thank you very much! Your point about loadimage made me think and i asked my colleagues. I managed to fix the issue. It was because I had the listview i created had a height set to wrap_content that the Listview was remaking itself every time a new row was added! – SKato Nov 16 '11 at 13:20