I have the following custom class to fetch news from a few RSS feeds like New York Times, BBC, etc.:
class News{
String title;
String link;
String imageURL;
}
This is the code I use to parse XML data:
void getRSSList() {
newsArray = new ArrayList<News>();
// Load each RSS feed URL in a for loop
for (int i = 0; i < catURLsList.size(); i++) {
String feedURL = catURLsList.get(i);
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL aUrl = new URL(feedURL);
InputStream is = getInputStream(aUrl);
parseRssFeeds(is);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
void parseRssFeeds(InputStream is) {
News n = new News();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
// Get LINK
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
n.link = xpp.nextText();
Log.i("log-", "LINK: " + n.link);
}
// Get TITLE
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
n.title = xpp.nextText();
Log.i("log-", "TITLE: " + n.title);
}
// Get MEDIA URL
} else if (xpp.getName().equalsIgnoreCase("media:content")) {
if (insideItem)
n.imageURL = xpp.getAttributeValue(null, "url");
Log.i("log-", "MEDIA URL: " + n.imageURL);
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
// Add news objects to the newsArray
newsArray.add(n);
eventType = xpp.next(); // move to next element
} // end WHILE loop
} catch(Exception e) { e.printStackTrace(); }
setNewsGridView();
}
setNewsGridView() calls a method with a GridAdapter inside which shows the title and image of RSSfeeds, the problem is that i get all titles, links and media URL's in my Logcat, but I get only the one news feed repeated in each cell of my GridView, as may times as the newsArray's size.
This is my GridAdapter:
// MARK: - SET NEWS GRID VIEW ---------------------------------------------
void setNewsGridView() {
class GridAdapter extends BaseAdapter {
private Context context;
public GridAdapter(Context context, List<News> objects) {
super();
this.context = context;
}
// CONFIGURE CELL
@Override
public View getView(int position, View cell, ViewGroup parent) {
if (cell == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cell = inflater.inflate(R.layout.cell_news, null);
}
// Get News object
News n = newsArray.get(position);
// Get Title
TextView titleTxt = (TextView) cell.findViewById(R.id.cnTitleTxt);
titleTxt.setText(n.title);
// Get Image
ImageView newsImg = (ImageView)cell.findViewById(R.id.cnImage);
if (n.imageURL != null) {
Picasso.with(context).load(n.imageURL).into(newsImg);
} else { newsImg.setImageResource(R.drawable.logo); }
return cell;
}
@Override public int getCount() { return newsArray.size(); }
@Override public Object getItem(int position) { return newsArray.get(position); }
@Override public long getItemId(int position) { return position; }
}
// Init GridView and set its adapter
GridView aGrid = (GridView) findViewById(R.id.hNewsGridView);
aGrid.setAdapter(new GridAdapter(Home.this, newsArray));
// Set number of Columns accordingly to the device used
float scalefactor = getResources().getDisplayMetrics().density * 150; // 150 is the cell's width
int number = getWindowManager().getDefaultDisplay().getWidth();
int columns = (int) ((float) number / (float) scalefactor);
aGrid.setNumColumns(columns);
}
My Logcat when I run the app:
I/log-: TITLE: Senate Votes Down Broad Obamacare Repeal
I/log-: LINK: https://www.nytimes.com/2017/07/25/us/politics/senate-health-care.html?partner=rss&emc=rss
I/log-: MEDIA URL: https://static01.nyt.com/images/2017/07/26/us/26dc-health-sub1/26dc-health-sub1-moth.jpg
I/log-: TITLE: John McCain to Senate: ‘We’re Getting Nothing Done’
I/log-: LINK: https://www.nytimes.com/video/us/politics/100000005305566/john-mccain-health-bill-vote.html?partner=rss&emc=rss
I/log-: TITLE: McCain Returns to Cast Vote to Help the President Who Derided Him
I/log-: LINK: https://www.nytimes.com/2017/07/25/us/politics/mccain-health-care-brain-cancer
etc...
...
And this is the output I get on the device:
What am I doing wrong?
Thanks so much!