RSS ListView, getting the right URL

Put your problem here if it does not fit any of the other categories.

RSS ListView, getting the right URL

Postby Kalder » Wed Jan 25, 2012 12:59 am

Hello guys,

I'm still working on my (simple) RSS Reader app.
I have it working as far as that it displays the listview with the items, which displays an image if there is one and the article title.
Now I managed to make the items go to an URL page, however whatever item i click it allways gets the URL of the last item in the list.

here is the RssReaderActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.tmm.android.rssreader;
  2.  
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.json.JSONObject;
  8.  
  9. import android.app.ListActivity;
  10. import android.content.Intent;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.widget.ListView;
  15.  
  16. import com.tmm.android.rssreader.reader.RssReader;
  17. import com.tmm.android.rssreader.util.Article;
  18.  
  19. public class RssActivity extends ListActivity {
  20.  
  21.         private RssListAdapter adapter;
  22.  
  23.         /** Called when the activity is first created. */
  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.  
  28.                 List<JSONObject> jobs = new ArrayList<JSONObject>();
  29.                 jobs = RssReader.getLatestRssFeed();
  30.  
  31.                 adapter = new RssListAdapter(this, jobs);
  32.                 setListAdapter(adapter);
  33.  
  34.         }
  35.  
  36.         @Override
  37.         protected void onListItemClick(ListView l, View v, int position, long id) {
  38.                 // TODO Auto-generated method stub
  39.                 super.onListItemClick(l, v, position, id);
  40.                 URL webURL = Article.url;
  41.  
  42.                 Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL
  43.                                 .toString()));
  44.  
  45.                 startActivity(myIntent);
  46.  
  47.         }
  48.  
  49. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



this is a part of the article class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public URL getUrl() {
  2.                 return url;
  3.         }
  4.  
  5.         /**
  6.          * @param url
  7.          *            the url to set
  8.          */
  9.         public void setUrl(URL url) {
  10.                 Article.url = url;
  11.                 // articleURL[RSSHandler.articlesAdded] = url;
  12.                 Log.d("Logging URL", "URL= " + url);
  13.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I have given a shot at making an array, but I lack the knowledge of how to use those, still I fear I have no other choice then to use an array of some sort to get this working.

Any help would be great!
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Top

Re: RSS ListView, getting the right URL

Postby Phyll » Wed Jan 25, 2012 3:17 am

Hi Kalder,

How's come this part of the onListItemClick event doesn't have anything to do with the postion that was returned? Could that be a problem?

Code: Select all
     URL webURL = Article.url;
     Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL.toString()));
     startActivity(myIntent);


Seems like it would always give you whatever the last value of Article was.

Hope this helps.

Phyll

PS

How did you make out with your splash screen and progress bar?
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: RSS ListView, getting the right URL

Postby Kalder » Wed Jan 25, 2012 3:42 pm

I have not continued working on a splash screen or progress bar, I am now focusing on getting the right url on clicking a list item, it's giving me a headache.
I now came to the part that I included the url in the JSONObject for each article which sets the right url for each article, but now i still don't know how to get the url from the clicked article.
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Re: RSS ListView, getting the right URL

Postby Phyll » Wed Jan 25, 2012 11:21 pm

Hi Kalder,

Every listview is backed up by an arraylist. Yours should have an array of objects which contain the url that you want. I assume it is an array of articles stored as json? At any rate you should use the position that is returned in the onclick listener for your list, to index to the object in the array, that has the url you want. I'm not sure what your code looks like so its hard to give you an exact example but it should look something like this:

Code: Select all
@Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
                super.onListItemClick(l, v, position, id);
                URL webURL =  jobs[position].getArticleUrl();    //whatever its called
                 Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL
                               .toString()));                           //if this is what it needs to be
                 startActivity(myIntent);


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: RSS ListView, getting the right URL

Postby Kalder » Wed Jan 25, 2012 11:28 pm

Wow, I feel so smart right now :D
I added a URL array in the RSSHandler.java which gives me the opportunity to call the correct url by calling to the url array at the position of the clicked listitem... (sorry if i sound confusing, it is confusing to explain but easy to understand i think)
For those interested:

added this to the RSSReader class:
Code: Select all
public static URL[] UrlArray = new URL[ARTICLES_LIMIT];


and this is what happens in the endelement void:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (localName.equalsIgnoreCase("item")) {
  2.  
  3.                         articleList.add(currentArticle);
  4.                        
  5.                         UrlArray[articlesAdded] = currentArticle.getUrl();
  6.                         Log.d("UrlArray TEST","URL nr: " + articlesAdded + "... URL: " + UrlArray[articlesAdded]);
  7.                        
  8.                         currentArticle = new Article();
  9.  
  10.                         // Lets check if we've hit our limit on number of articles
  11.                         articlesAdded++;
  12.                         if (articlesAdded >= ARTICLES_LIMIT)
  13.                         {
  14.                                 throw new SAXException();
  15.                         }
  16.                 }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

and finally to get the right url on click in the end of the oncreate method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.     protected void onListItemClick(ListView l, View v, int position, long id) {
  3.             // TODO Auto-generated method stub
  4.             super.onListItemClick(l, v, position, id);
  5.                
  6.            
  7.             //URL webURL = RssListAdapter.jsonImageText.get();
  8.            
  9.             URL webURL = RSSHandler.UrlArray[position];
  10.                         Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL.toString()));
  11.  
  12.             startActivity(myIntent);
  13.            
  14.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests