简体   繁体   中英

Conditionally changing list adapter android

I'm working on an app for Android using the Facebook SDK. So far, the app scans a certain pages feed and puts all the posts into a GridView. I want it so that if the value of the imageArray at that current position is empty or null (A post without a picture), then it instead sets the ImageView visibility to GONE and makes the TextView take up the entire item. Here's my code:

public View getView(int position, View v, ViewGroup parent) {

    MainListHolder mHolder;
    if (v == null) {
        mHolder = new MainListHolder();
        v = inflator.inflate(R.layout.list_item, null);
        mHolder.img1 = (ImageView) v.findViewById(R.id.preview);
        mHolder.txt2 = (TextView) v.findViewById(R.id.message);
        mHolder.txt3 = (TextView) v.findViewById(R.id.user_name);
        mHolder.txt4 = (TextView) v.findViewById(R.id.user_id);
        v.setTag(mHolder);
    } else {
        mHolder = (MainListHolder) v.getTag();
    }

    if (!imageArray.get(position).isEmpty()) {
        manager.DisplayImage(imageArray.get(position), loader, mHolder.img1);
    }
    else    {
        manager.DisplayImage(null, loader, mHolder.img1);
        mHolder.img1.setVisibility(ImageView.GONE);
    }
    mHolder.txt2.setText(messageArray.get(position));
    mHolder.txt3.setText(nameArray.get(position));
    mHolder.txt4.setText(idArray.get(position));

    return v;
}

The problem is the GridView now changes its items while I'm scrolling. Even if the item has a picture, the ImageView disappears. How do I fix this?

Your problem is in this part of code:

if (!imageArray.get(position).isEmpty()) {
    manager.DisplayImage(imageArray.get(position), loader, mHolder.img1);
}
else {
    manager.DisplayImage(null, loader, mHolder.img1);
    mHolder.img1.setVisibility(ImageView.GONE);
}

So if image for imageView doesn't exists you make img1 GONE. But if image exists img1 still remains GONE after scrolling, because you are not calling mHolder.img1.setVisibility(ImageView.VISIBLE). And this happens because when you are scrolling, your listItem views are recycling, so in adapter you have to treat ALWAYS both conditions.

So your code shold look like this:

if (!imageArray.get(position).isEmpty()) {
    mHolder.img1.setVisibility(ImageView.VISIBLE);
    manager.DisplayImage(imageArray.get(position), loader, mHolder.img1);
}
else {
    manager.DisplayImage(null, loader, mHolder.img1);
    mHolder.img1.setVisibility(ImageView.GONE);
}

Example for better understanding of problem:

Imagine this code in list adapter getView() mehod:

if(isEnabled) {
   textView.setText("ENABLED");
}

In this case textView will always show text ENABLED after scrolling, because there is no else part where textview will show some other text. So code should look like:

if(isEnabled) 
   textView.setText("ENABLED");
else
   textView.setText("DISABLED");

Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM