1

I wanted to study android sqlite so I created an instant tagging application in which a user can tag his photos and upload them to his network (google+, fb, twitter) problem is I have a ListFragment in which the user can see all the tagging he made, the image elements in the list are stored on a hidden folder and the words are stored in a sqlite db.. problem is while scrolling up or down some items just switch randomly plus even though I have more than 8 items them 8 first items are shown repeatedly (i.e 1-8 than instead of 9-12 I see 1-4 again) the only problem might be in the adapter but after sessions on sessions of debug I fail to find the problem my code for the adapter is -

public class FlowAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, List<String>>> data;
    private static LayoutInflater layoutInflater = null;

    public FlowAdapter(Activity activityContext,
            ArrayList<HashMap<String, List<String>>> data) {
        this.activity = activityContext;
        this.data = data;
        this.layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // method variables
        ViewHolder cacheView;
        HashMap<String, List<String>> photos = null;
        photos = data.get(position);
        ImageView iv;
        FlowLayout flow;

        ArrayList<String> subjects = new ArrayList<String>();

        if (convertView == null) {
            cacheView = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            flow = (FlowLayout) convertView.findViewById(R.id.flow_tags);;

            // add the tags to the flowlayout
            int size = photos.get(DatabaseHandler.KEY_TAGS).size();
            for (int i = 0; i < size; i++) {
                String name = String.format("#%s",
                        photos.get(DatabaseHandler.KEY_TAGS).get(i));
                Bubble.getBubble(name, flow, subjects, activity, photos
                        .get(DatabaseHandler.KEY_THUMBNAILPATH).get(1), false);
            }
            cacheView.image = (ImageView)convertView.findViewById(R.id.list_image);//iv;
            cacheView.tags = flow;
            convertView.setTag(cacheView);

        }   

        cacheView = (ViewHolder) convertView.getTag();
        cacheView.image.setImageBitmap(null);
        DecodeTask task = new DecodeTask(cacheView.image);
        task.execute(photos.get(DatabaseHandler.KEY_THUMBNAILPATH).get(1));
        cacheView.image.setTag(R.id.list_image, task);

        return convertView;
    }

    static class ViewHolder {
        static FlowLayout  tags;
        static ImageView image;

        public static FlowLayout getFlowLayout() {
            return tags;
        }
    }
}

The flow layout is from here - http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/ The bubble layout is from here - http://nishantvnair.wordpress.com/2010/09/28/android-create-bubble-like-facebook/ I use this SO thread to load the images on the background - Large ListView containing images in Android any help? =\ p.s I know there are already apps like that [ and alot actually, but the best learning comes while writing code so thats what I did =) ]

Community
  • 1
  • 1
crazyPixel
  • 2,301
  • 5
  • 24
  • 48
  • How about placing this line **cacheView = (ViewHolder) convertView.getTag();** inside an else condition? Not sure if it will help, but you could give it a try? – Rat-a-tat-a-tat Ratatouille Oct 13 '13 at 12:52
  • Still no effect...I suspect it somehow has a connection to the flowlayout or the bubbleview since they are dynamic (in other words there is no id to which I attach the bubble layout I just add them to the flowlayout on runtime - take a look on their implemetation on the links I added) – crazyPixel Oct 13 '13 at 19:26

1 Answers1

4

Reason is obvious !!! You have static variables in ViewHolder:

Instead of:

 static class ViewHolder {
    static FlowLayout  tags;
    static ImageView image;

    public static FlowLayout getFlowLayout() {
        return tags;
    }
}

It must be:

 static class ViewHolder {
     FlowLayout  tags;
     ImageView image;

    public FlowLayout getFlowLayout() {
        return tags;
    }
}

More on static variables is available on internet, in short they are not instance variables so changing its value will change it for every instance (Although static variable should be accessed using class reference).

UPDATE:

You also need to cancel your DecodeTask if convertView is null as the tutorial says: Large ListView containing images in Android

But keeping those variables static is as simple mistake and easy to detect as you can detect a mistake in 2+2 = 5. Saying that removing static modifier seems to broke everything means that something else is wrong with your code.

Community
  • 1
  • 1
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • not working...it even got worse.. =\ before using static there was one elemtn on the whole list "misbehaving" and some just didnt showed up, now there is not even one image in its right place (next to the relevant description)... – crazyPixel Oct 10 '13 at 18:37
  • `even got worse` doesn't necessarily means that you did something wrong. This clearly shows that photo loading code is wrong some where. As making variables static seems its working. On a second look I was able to identify that you are not cancelling `DecodeTask ` as suggested by tutorial in you `else` in `getView()`. I will update answer shortly to point it more clearly. – M-Wajeeh Oct 11 '13 at 07:48