Darcy
Darcy

Reputation: 5368

Extending BaseAdapter for ListView getView updating the view improperly

I have a RSS feed viewer and wanted to show the user the last time they listened to a particular feed.

This works for the first items loaded in the list (the items that are visible in the current view), but as the user scrolls down the listview, the lastViewedText for every other viewed feed is hidden. Additionally, when the user scrolls back up to the top (where the lastViewedText was successfully displayed before) the lastViewedText is now hidden. The BaseAdapters getView method is where I set the visibility.

Custom BaseAdapter:

public class PodcastAdapter extends BaseAdapter{

    private ArrayList<Podcast> feed;
    private LayoutInflater inflater = null;

    public PodcastAdapter(Activity a, ArrayList<Podcast> feed){
        this.feed = feed;
        inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView == null)
            vi = inflater.inflate(R.layout.rss_listview_item, null);

        TextView title = (TextView)vi.findViewById(R.id.title);
        TextView desc = (TextView)vi.findViewById(R.id.description);
        TextView date = (TextView)vi.findViewById(R.id.date);
        TextView lastViewedText = (TextView)vi.findViewById(R.id.lastViewedText);

        Podcast feedIndex = feed.get(position);

        title.setText(feedIndex.Title);
        desc.setText(feedIndex.Description);
        date.setText(feedIndex.Date);
        lastViewedText.setText(feedIndex.LastViewedText);


        if(feedIndex.LastViewedText.equals("")){
            lastViewedText.setVisibility(View.GONE);
        }

        return vi;
    }
}

Why is the view not updating properly? The title, desc, date are all loaded properly!

Upvotes: 0

Views: 902

Answers (2)

Bartek Filipowicz
Bartek Filipowicz

Reputation: 1235

   if(feedIndex.LastViewedText.equals("")){
            lastViewedText.setVisibility(View.GONE);
        } else {
            lastViewedText.setVisibility(View.VISIBLE);
    }

The view is hidden in the convert view, and you dont unhide it:)

Also, you can use TextUtils.isEmpty(CharSequence string), to check if a String is empty or null.

Upvotes: 2

Budius
Budius

Reputation: 39846

do that instead:

    if(feedIndex.LastViewedText.equals("")){
        lastViewedText.setVisibility(View.GONE);
    }else{
        lastViewedText.setVisibility(View.VISIBLE);
    }                  

Upvotes: 1

Related Questions