I am trying to implement dynamic ListView in my program which gets parsed values. Now I want to add those parsed values in that ListView by the help of a loop and I am helpless here. I had tried so many examples but failed so please someone help me. because I really need help. here is the piece of code.
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);
}
}
});
}
}
this code should work :
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);