Reputation: 1243
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);
}
}
}
Upvotes: 2
Views: 1620
Reputation: 5773
Your code cannot work because you are always replacing your textview with the last massage information. you should do this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statisticsscreen);
tv = (TextView) findViewById(R.id.xmltextview);
loadFeed();
private void loadFeed() {
try {
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
StringBuilder builder = new StringBuilder();
for (Message msg : messages){
builder.append(msg.getTemperature());
builder.append(",");
builder.append(msg.getRain());
builder.append(",");
builder.append(msg.getWind());
builder.append("\n");
}
tv.setText(builder.toString());
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}
}
Upvotes: 2
Reputation: 234795
You'll need to format your data in some way and then use TextView's setText()
method. If you want HTML-like formatting, you will find the android.text.Html.fromHtml()
method useful.
Upvotes: 1