1

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;
    }
}
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
Rick
  • 3,943
  • 6
  • 33
  • 45

1 Answers1

2

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.

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • Thanks for your answer but I meant that I want the title for the item of the list view (in BOLD perhaps) and description and others for subItem of the listView – Rick Oct 22 '13 at 15:00
  • I'm not sure I follow you, but this code allows you to do that. In your `YourView` constructor, you can pull the title out of `YourDataObject` and style it as bold, and style the description differently. Remember, the view you return from `getView()` can contain any number of nested views, and you can even inflate a layout for this view and stick values from your data object into it. – Sky Kelsey Oct 22 '13 at 17:51
  • Excuse me for being annoying, but I just can not understand. Could you give me an example of getting the description. I don't get results – Rick Oct 22 '13 at 20:49
  • 1
    Can you please edit your question to include a few pieces of well-formatted code? 1) The class holding the data you wish to show. 2) A general description of the layout you wish to achieve in each list item, or an actual layout.xml, if possible. – Sky Kelsey Oct 22 '13 at 21:35
  • What does it mean? Isn't it? – Rick Oct 24 '13 at 13:29
  • I formatted your code to make it readable. I also edited my answer to reflect what changes you should make to your code to accomplish your goal. – Sky Kelsey Oct 24 '13 at 20:05
  • Sorry but I can't understand. What is YourDataObject and YourView? I have to create a new class named YourArrayAdapter? – Rick Oct 28 '13 at 19:55
  • I use the word "Your" when I'm naming classes that you need to create. You should be able to just pick a name, and use the rest of the code. – Sky Kelsey Oct 28 '13 at 20:06