crazyPixel
crazyPixel

Reputation: 2330

Strange bug in listfragment

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 =) ]

Upvotes: 1

Views: 243

Answers (1)

M-Wajeeh
M-Wajeeh

Reputation: 17284

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.

Upvotes: 4

Related Questions