所以我有两个关于这个 ArrayAdapter 的问题:
1.构造函数中的第三个参数List<RSSItem> listgetView中自动解析,position会遍历这个list中的每一项? (所以唯一重要的事情是调用 super 将此列表作为参数传递?)

2.在代码的最后,我们有 new MyCustomAdapter(this, R.layout.row, myRssFeed.getList()); 这怎么能工作,而不是在代码中创建一个无限循环?因为在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”怎么可以为空?
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) 可以对来自您提供的 return 的特定位置的项目进行 List 。另一种方法 int getCount() 将告诉 adapter 有多少项目是他们的。
  • 他们不是无限循环。 ArrayAdapter 简单地调用 super 以利用继承的属性以及 getItem()getCount()
  • 两种方法

    关于android - arrayAdapter = 无限循环? (参见 : code),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8723085/

    10-12 03:28