Reputation: 6176
So i have 2 questions about this ArrayAdapter :
1. the third parameter List<RSSItem> list
in the constructor is automatically analysed in getView
, and position
will iterate through each item in this list
? (so the only important thing to do is call super
to pass this list as a parameter?)
2.at the end of the code, we have new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
how this can work, and not create an infite loop in the code? because at the end of the arrayAdapter, the class calls itself to restart the adapter... how does the adapter end?
here's the code (source: http://android-er.blogspot.com/2010/07/simple-rss-reader-with-options-menu-to.html ) :
public class AndroidRssReader extends ListActivity {
private RSSFeed myRssFeed = null;
TextView feedTitle;
TextView feedDescribtion;
TextView feedPubdate;
TextView feedLink;
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position%2 == 0){
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
}
else{
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return row;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
feedTitle = (TextView)findViewById(R.id.feedtitle);
feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
feedPubdate = (TextView)findViewById(R.id.feedpubdate);
feedLink = (TextView)findViewById(R.id.feedlink);
readRss();
}
private void readRss(){
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
feedPubdate.setText("");
feedLink.setText("");
setListAdapter(null);
Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
try {
URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (myRssFeed!=null)
{
Calendar c = Calendar.getInstance();
String strCurrentTiime = "\n(Time of Reading - "
+ c.get(Calendar.HOUR_OF_DAY)
+ " : "
+ c.get(Calendar.MINUTE) + ")\n";
feedTitle.setText(myRssFeed.getTitle() + strCurrentTiime);
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter =
new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
EDIT : in this example, how "view" can be null?
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, List<String> objects) {
super(context, R.layout.list_item, R.id.text, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Wrapper wrapper;
if (view == null) {
view = mInflater.inflate(R.layout.list_item, null);
wrapper = new Wrapper(view);
view.setTag(wrapper);
} else {
wrapper = (Wrapper) view.getTag();
}
Thanks
Upvotes: 3
Views: 1236
Reputation: 1835
Change
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
To this.
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(((RSSItem)getItem()).getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(((RSSItem)getItem()).getPubdate());
Use getItem()
instead of the list. The adapter wont go to infinite loop.
Though it is not necessary for ArrayAdapter try to use Base adapter and implement getItem()
and getCount()
methods.
Upvotes: 0
Reputation: 37729
Yea! there is a method getItem(int position)
that will return
the item at that specific position from your provide List
. and another method int getCount()
will tell the adapter
about how many items are their.
their is no infinite loop. the ArrayAdapter
simply calls super
to avail the inherited properties and both methods getItem()
and getCount()
Upvotes: 4
Reputation: 1934
To understand this, try using "BaseAdapter". It is the stock adapter provided in Android, with the very basic functionality and you need to specify all things in it.
But in nutshell, the no of times an adapter will produce views for rows of the list equals to the no of items in the list.
There is a method "getCount()" provided by the BaseAdapter which determines how many rows would be populated by the adapter in total (excluding the concept of efficient adapter ie. reusability of views, to keep explanation simple for you).
In case of "ArrayAdapter" when you call its super with providing list of items to it, the "getCount()" is called iteself and the no is determined by the adapter itself, generating same no of views.
Upvotes: 1