At the moment my parser parses data and stores it in a listView which can be displayed easily.
However I want to use a textView to display this data instead but I’m not sure how.
Here’s the code that works using a listview:
public class StatisticsScreen extends ListActivity{
private List<Message> messages;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statisticsscreen);
loadFeed();
lv = (ListView) findViewById(android.R.id.list);
private void loadFeed() {
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
titles.add(msg.getTemperature());
titles.add(msg.getRain());
titles.add(msg.getWind());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.xmldatarow,titles);
getListView().addHeaderView(View.inflate(this, R.layout.xmllayout, null));
this.setListAdapter(adapter);
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}
So like I said this works perfectly, but I want to use a TextView instead of a ListView to store/display the data..
Any advice?
EDIT: HERE’S THE SOLUTION
Here’s the updated code that works now:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statisticsscreen);
tv = (TextView) findViewById(R.id.xmltextview);
tv1 = (TextView) findViewById(R.id.xmltextview1);
tv2 = (TextView) findViewById(R.id.xmltextview2);
loadFeed();
private void loadFeed() {
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
for (Message msg : messages){
tv.setText(msg.getTemperature());
tv1.setText(msg.getRain());
tv2.setText(msg.getWind());
}
} catch (Throwable t){
Log.e("Weather",t.getMessage(),t);
}
}
}
Your code cannot work because you are always replacing your textview with the last massage information. you should do this: