0

我正在尝试在我的程序中实现动态 ListView 来获取解析值。现在我想通过循环的帮助在那个 ListView 中添加那些解析的值,我在这里很无助。我尝试了很多例子,但都失败了,所以请有人帮助我。因为我真的需要帮助。这是一段代码。

    package com.ifahja.cricketpredictor;

    import java.net.URL;

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;




    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.AdapterView.OnItemClickListener;

    public class ScorecardActivity extends Activity
    {
     ListView listView;

         public void onCreate(Bundle savedInstanceState)
         {
              super.onCreate(savedInstanceState);


              setContentView(R.layout.scorecard);

              ScorecardGetterSetter scorecard;

              listView=(ListView)findViewById(R.id.listView1);

          try
        {
            URL url;
            String queryString1 ="http://static.cricinfo.com/rss/livescores.xml";

            /* Replace blanks with HTML-Equivalent. */  

            url = new URL(queryString1.replace(" ", "%20"));

            /* Get a SAXParser from the SAXPArserFactory. */

            SAXParserFactory spf = SAXParserFactory.newInstance();

            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();

            ScorecardHandler myHandler = new ScorecardHandler();

            xr.setContentHandler(myHandler);

            /* Parse the xml-data our URL-call returned. */
            xr.parse(new InputSource(url.openStream()));

            Log.d("URL", "it's all right");
        } 

        catch (Exception e) 
        {
            System.out.println(e);
            Log.d("URL", "problum in URL");

        }

        scorecard = ScorecardHandler.scorecard;



        //for(int i=1;i<scorecard.getTitle().size();i++)
        //{
        //TextView title = new TextView(this);
        final String android_versions[] = {
                scorecard.getTitle().get(1),
                scorecard.getTitle().get(2),
                scorecard.getTitle().get(3),
                scorecard.getTitle().get(4),
                scorecard.getTitle().get(5),
                scorecard.getTitle().get(6),
        //                  scorecard.getTitle().get(7),
       //                   scorecard.getTitle().get(8),
       //                   scorecard.getTitle().get(9),
       //                   scorecard.getTitle().get(10),
       //                   scorecard.getTitle().get(11),
    `enter code here`//                 scorecard.getTitle().get(12),
    //                  scorecard.getTitle().get(13),
    //                  scorecard.getTitle().get(14),
     //                 scorecard.getTitle().get(15),
   //                   scorecard.getTitle().get(16),
   //                   scorecard.getTitle().get(17),
   //                   scorecard.getTitle().get(18),
   //                   scorecard.getTitle().get(19),

        };


         ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,   R.layout.custom_list_item,android_versions);             
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                if(android_versions[arg2]==android_versions[0])
                {
                    Intent intentList_0 = new            Intent(ScorecardActivity.this,List_0.class);
                    startActivity(intentList_0);
                }
                }

                });

        }

         }
4

1 回答 1

2

这段代码应该可以工作:

ListView listView = (ListView) findViewById(R.id.mylist);



List< String> scorecard = new ArrayList< String>(); \\\\\\\\ the trick is here 

String[] values = new String[scorecard .size()];

for (int i = 0; i < scorecard.size(); i++) {
    values[i] = scorecard.get(i).getName();
}
ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);
于 2013-03-03T11:34:37.807 回答