2012-01-04 56 views
3

所以我对这个ArrayAdapter 2个问题:
1.在构造函数中的第三个参数List<RSSItem> list自动分析了getView,并position将通过在本list每个项目重复? (所以唯一重要的事情就是打电话super通过这个列表作为参数?)arrayAdapter =无限循环? (参见:代码)

2.在代码的末尾,我们有new 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); 

} 
    } 

编辑: 在这个例子中,如何 “视图” 可以为空?

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(); 
     } 

感谢

回答

4
  1. 是的!有一种方法getItem(int position)即将return该物品在您提供的那个特定位置List。另一种方法int getCount()将告知adapter它们有多少项目。

  2. 他们没有无限循环。在ArrayAdapter只需调用super援用继承的属性和方法都和getItem()getCount()

+0

感谢Adil,好吧现在对我更加清楚,非常感谢 – Paul 2012-01-04 08:58:49

+0

Adil,我可以问你一些其他问题:在我的编辑中,如何查看可以为空?我们通过'super'调用传递了'R.layout.list_item',那么视图如何可以是“空”?谢谢 – Paul 2012-01-04 09:57:06

+1

+1很好的解释。 – 2012-01-04 09:57:23

1

要理解这一点,请尝试使用 “BaseAdapter”。 它是Android中提供的股票适配器,具有非常基本的功能,您需要指定其中的所有内容。

但简而言之,适配器生成列表行视图的次数等于列表中的项目数量。

BaseAdapter提供了一个方法“getCount()”,它确定适配器总共将填充多少行(不包括高效适配器的概念,即视图的可重用性,以便为您解释简单) 。

如果“ArrayAdapter”在您向其提供项目列表的情况下调用它的super时,“getCount()”被称为iteself,no由适配器本身确定,生成相同数量的视图。

+1

感谢akkilis为您解答(您的昵称是对特洛伊的一个参考?) – Paul 2012-01-04 08:59:53

0

变化

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()); 

了这一点。

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()); 

使用getItem(),而不是名单。适配器不会进入无限循环。 虽然ArrayAdapter不需要使用Base适配器并实现getItem()getCount()方法。

+0

感谢Santhosh的回答 – Paul 2012-01-04 09:00:35