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

时间:2012-01-04 06:48:39

标签: android android-arrayadapter

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

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

 }
  }

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

由于

3 个答案:

答案 0 :(得分:4)

  1. 是啊!有一种方法getItem(int position)return来自您提供List的特定位置的项目int getCount()。另一种方法adapter会告诉ArrayAdapter他们有多少项目。

  2. 他们没有无限循环。 super只需调用getItem()即可使用继承的属性,方法getCount()和{{1}}

答案 1 :(得分:1)

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

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

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

如果是“ArrayAdapter”,当你调用它的超级项目提供项目列表时,“getCount()”被称为iteself,而no由适配器本身确定,产生相同的no视图。

答案 2 :(得分: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()方法。

相关问题