android–arrayAdapter =无限循环? (cf:代码)

android–arrayAdapter =无限循环? (cf:代码),第1张

概述所以我对这个ArrayAdapter有2个问题:1.第三参数List<RSSItem>在getView中自动分析构造函数中的列表,并且位置将遍历此列表中的每个项目?(所以唯一重要的是调用super将这个列表作为参数传递?)2.在代码的末尾,我们有新的MyCustomAdapter(this,R.layout.row,myRssFeed.getList());这

所以我对这个ArrayAdapter有2个问题:
1.第三参数List< RSSItem>在getVIEw中自动分析构造函数中的列表,并且位置将遍历此列表中的每个项目? (所以唯一重要的是调用super将这个列表作为参数传递?)

2.在代码的末尾,我们有新的MyCustomAdapter(this,R.layout.row,myRSSFeed.getList());这是如何工作的,而不是在代码中创建一个infite循环?因为在arrayAdapter的末尾,类调用自身重新启动适配器…适配器如何结束?

这是代码(来源: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); }  }

编辑:
在这个例子中,“vIEw”如何为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();        }

谢谢

解决方法:

>是的!有一个方法getItem(int position),它将从你的提供List返回该特定位置的项目.另一个方法int getCount()将告诉适配器他们有多少项.
>他们没有无限循环. ArrayAdaptersimply调用super来利用继承的属性,并使用getItem()和getCount()方法

总结

以上是内存溢出为你收集整理的android – arrayAdapter =无限循环? (cf:代码)全部内容,希望文章能够帮你解决android – arrayAdapter =无限循环? (cf:代码)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105564.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存