2012-01-04 8 views
3

だから私はこのArrayAdapterについて2つの質問があります。
1.コンストラクタで三番目のパラメータList<RSSItem> listは自動的getViewで分析され、そしてpositionは、このlistの各項目を反復処理しますか? (このリストをパラメータとして渡すには、superを呼び出してください)arrayAdapter =無限ループですか? (CF:コード)

2.コードの最後には、new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());があります。これはどのように動作し、コードに無限ループを作成しません? arrayAdapterの終わりで、クラ​​スはアダプタを再起動するために自身を呼び出します...アダプタはどのように終了しますか?

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:ここ

コード(:http://android-er.blogspot.com/2010/07/simple-rss-reader-with-options-menu-to.htmlソース)です この例では、どのように "ビュー" nullにすることができますか?

答えて

4
  1. Yea!方法getItem(int position)があり、Listからその特定の位置の商品をreturnにします。もう1つの方法int getCount()は、どれくらいの数のアイテムがあるかをadapterに伝えます。

  2. 無限ループではありません。 ArrayAdapterは、単純に継承されたプロパティとメソッドの両方getItem()、これにgetCount()

+0

ありがとうございましたAdil、大丈夫よ、今よりもっと私には分かります – Paul

+0

Adil、私はあなたに何か他のことを聞くことができますか?私の編集では、どのように "view"がnullでもかまいませんか?私たちは 'super'呼び出しを通して' R.layout.list_item'を渡したので、どのようにビューが "null"になることができますか?ありがとう – Paul

+1

+1良い説明。 –

1

はこれを理解するには

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

おかげで、 "BaseAdapter" を使用してみてください。 Androidには、非常に基本的な機能を備えたストックアダプタがあり、その中にすべてのものを指定する必要があります。

しかし、一言で言えば、アダプターはリストの行のビューをリスト内の項目の数と同じにします。

BaseAdapterによって提供されるgetCount()メソッドがあります。このメソッドは、アダプターによって合計数が入力される行数を決定します(効率的なアダプター、すなわちビューの再利用性、 。

"ArrayAdapter"の場合、アイテムのリストを提供するスーパークラスを呼び出すと、 "getCount()"はiteselfと呼ばれ、noはアダプタ自体によって決定され、同じビュー数を生成しません。

+1

あなたの答えに感謝のakkilis(あなたのニックネームはTroyのリファレンスですか?) – Paul

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

を利用するためにsuperを呼び出します。

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