Question

In my list view for ITEM I get the title of the feed but I want to get the description, image and link in the SUBITEM in that list view. Can you help me? This is what I have:

1) The ListView in MainActivity

ArrayAdapter<RSSItem> adapter;
adapter = new ArrayAdapter<RSSItem>(
    this,
    android.R.layout.simple_list_item_1,
    myRssFeed.getList()
);
setListAdapter(adapter);

2) RSSItem

public class RSSItem {

    private String title = null;
    private String description = null;
    private String link = null;
    private String pubdate = null;

    RSSItem(){}

    void setTitle(String value) {
        title = value;
    }

    void setDescription(String value) {
        description = value;
    }
    void setLink(String value) {
        link = value;
    }
    void setPubdate(String value) {
        pubdate = value;
    }

    String getTitle() {
        return title;
    }

    String getDescription() {
        return description;
    }
    String getLink() {
        return link;
    }
    String getPubdate() {
        return pubdate;
    }

    public String toString() {
        //TODO Auto-generated method stub
        return title;
    }
}
Was it helpful?

Solution

Just use a custom ArrayAdapter. It's super simple:

1) Define your custom ArrayAdapter. Fill in the body of getView() to create a view based on each item your pass to the adapter;

public class YourArrayAdapter<YourDataObject> extends ArrayAdapter<T> {

    public YourArrayAdapter(Context context) {
        super(context, 0); // Pass in 0 because we will be overriding getView()
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // getView() gets called when this item becomes visible in the ListView
        // All you have to do is build a view with your data object and return it.
        YourDataObject yourDataObject = getItem(position);
        YourView view = new YourView(yourDataObject);

    }
}

2) Pass the adapter to your ListView, and add data.

YourArrayAdapter<RSSItem> adapter = new YourArrayAdapter<RSSItem>(this);
adapter.addAll(myRssFeed.getList());
setListAdapter(adapter);

The important thing to realize here is that Adapters turn lists of data into actual UI Views when the ListView requests a view via Adapter.getView(). In this case, you are creating a custom ArrayAdapter, and so you control everything about the View that it returns. You can return a view that has its own layout, and includes the many different pieces of data that RSSItem includes, and presents them in whatever format you desire. The cool thing is that by using ListView and a custom ArrayAdapter, you never have to worry about creating or destroying these views -- that's all taken care of for you. So if the items your return from ArrayAdapter.getView() include bitmaps, you don't need to really worry too much about running out of memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top