0

I am writing a program that allows the user to provide the URL to RSS feeds, downloads and parses the information, and then displays it to JTextAreas.

  • The user can add as many feed categories as he/she wants. (each category is represented in a separate pane)
  • The user can add as many feeds in a category as he/she wants.
  • The user can either add a new feed or a new category.

I have an ArrayList(ArrayList) = categoryList. Each of it's items are category = ArrayList. And the Feed objects contain a getContents() method which returns ArrayList.

I want to associate every Feed with a JTextArea. Then I want to iterate through the FeedItems, while displaying them in the corresponding JTextArea.

I am really stuck on finding a way to associate a one to one map between Feed <=> JTextArea.

Here is my attempt:

if (category_list.size() > 0)
    {
        final java.util.Iterator<ArrayList<Feed>> categoryListIterator = category_list.iterator();
        final ArrayList<Feed> currCategory;
        final java.util.Iterator<Feed> currFeedIterator;

        while (categoryListIterator.hasNext())
        {
            currCategory = categoryListIterator.next();
            while (((java.util.Iterator<ArrayList<Feed>>) currCategory).hasNext())
            {
                ArrayList<Feed> currFeed = ((java.util.Iterator<ArrayList<Feed>>) currCategory).next();

                currFeedIterator = currFeed.iterator();
                while (currFeedIterator.hasNext())
                {
                    Feed feed = currFeedIterator.next();
                    final java.util.Iterator<FeedItem> feedIterator = feed.getContents().iterator();
                                            while (feedIterator.hasNext())
                                            {
                                              // find the appropriate JTextArea
                                              // iterate through the articles.

                                                 correspondingJtextArea.setText(feedIterator.next().toString());
                                            }

                }
            }
        }
    }

Please let me know, what would be the best way of keeping track in which JTextArea I should change the text.

Am I iterating through all the Feeds here correctly? A short example that illustrates my situation would be very very helpful.

UPDATE:

Okay, so now I'm okay iterating through all the feeds. I only need to find a way to write a HashMap which associates a Feed with a JTextArea.

Don Code
  • 781
  • 3
  • 12
  • 25

1 Answers1

1

Is there any identification associated with every feed? If yes, you can have a HashMap with key as id and value as JTextArea object. If no, then you can create a custom ID for each feed and use HashMap for associations. Regarding iterating your feeds, you need to use loops instead of ifs.

public static JTextArea getTextAreaByFeedId(int id)
{
    JTextArea correspondingJtextArea = id_txtareaMap.get(id); 
    if(correspondingJtextArea == null)
    {
        correspondingJtextArea = new JTextArea();
        id_txtareaMap.put(id, new JTextArea()); 
    }
    return correspondingJtextArea;
}
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
public static void main (String[] args) {
    HashMap<Integer, JTextArea> id_txtareaMap = new HashMap<>(); 
    if (category_list.size() > 0)
    {

        final java.util.Iterator<ArrayList<Feed>> categoryListIterator = category_list.iterator();
        final ArrayList<Feed> currCategory;
        final java.util.Iterator<Feed> currFeedIterator;

        while (categoryListIterator.hasNext())
        {
            currCategory = categoryListIterator.next();
            while (((java.util.Iterator<ArrayList<Feed>>) currCategory).hasNext())
            {
                ArrayList<Feed> currFeed = ((java.util.Iterator<ArrayList<Feed>>) currCategory).next();

                currFeedIterator = currFeed.iterator();
                while (currFeedIterator.hasNext())
                {
                    Feed feed = currFeedIterator.next();
                    final java.util.Iterator<FeedItem> feedIterator = feed.getContents().iterator();
                    while (feedIterator.hasNext())
                    {
                        // find the appropriate JTextArea
                        // iterate through the articles.
                        JTextArea correspondingJtextArea = getTextAreaByFeedId(FeedItem.getId());
                        correspondingJtextArea.setText(feedIterator.next().toString());
                    }

                }
            }
        }
    }
}
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
  • 2
    The only way I can tell feeds apart is if I call their methods. However, the user is allowed to add the same feed twice. I don't understand how to create a HashMap and how to create the Custom ID!!! Could you please provide some type of example, or explain this process in more detail? – Don Code Nov 30 '12 at 05:40
  • I didn't understand your first line. Is there any method which you can call on feeds to get any Id? If user adds the same feed twice, do you want 2 corresponding text areas? The Feed class should have Id (custom) field which you can fill (e.g. simple count which keeps incrementing) while creating objects and with the same Id you can create Hashmap with text areas. Like: `HashMap id_txtareaMap = new HashMap<>(); id_textareaMap.put(feed.getId(), new JTextArea());` – vishal_aim Nov 30 '12 at 06:00
  • I am creating a method called getId for my Feed class. Can you tell me what I do afterwards, so that all of my Feeds have ID's and all JTextArea's have ID's. Thanks! – Don Code Nov 30 '12 at 06:38
  • I cannot have an getId method in my Feed class. They follow the RSS schema and are created automatically by the parser. Is there any other way?????? Could you tell me how will I make sure that every Feed I add will be associated to a JTextArea. What I am asking is, if I am given a Feed. How will I link it to a JTextArea? – Don Code Nov 30 '12 at 06:48
  • You need to have that Id filled when creating feed object. You can maintain id_txtareaMap map to have corresponding text area. Does above code help you (updated your code and posted)? – vishal_aim Nov 30 '12 at 06:49
  • First please repair your shift key. I donot know your full code. You can have a kind of global hash map and write a method to retrieve it based on feed like added above and use it wherever you want, it'll updated itself if not in map already. – vishal_aim Nov 30 '12 at 07:15
  • you can also try to keep feed object as key in the hash map if having Id is not possible. Initialization is simple, just create hash map once and use method getTextAreaByFeed(Id or feed object) wherever you want to get/update that map with corresponding values – vishal_aim Nov 30 '12 at 07:31