-1

I noob in android and need help, and I have an online xml, I used this example to parse. example

But do not know how to add color to the content "Var" depending on the value, if higher than zero, this is green and if it is less than zero red.

Community
  • 1
  • 1
edwin rojas
  • 139
  • 2
  • 9

1 Answers1

0

Assuming this is the tutorial you've referring to http://www.jondev.net/articles/Android_XML_SAX_Parser_Example then i would assume that you've solved how to get the data into the listview already?

If you've used a customListAdapter i.e. as in this example http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx then you will need to add an additional step in the getView() method to customise the colour of your listitem view. For example, you could change the background colour or the text colour.

public View getView(int position, View convertView, ViewGroup parent) {
    ....
    TextView mytext = findViewById(R.id.myText);
    int yourVar = getItem(position).var;
    if(yourVar > 0)
    {
        mytext.setTextColor(0x0000FF);
    }else{
        mytext.setTextColor(0xFF0000);
    }
}

This assumes a certain amount, for example that your using a custom list adapter with your own layout.

Amendment. If this was a list of text in a textview, then you could try formatting it with html, for example this question shows how to colour text Change text color of one word in a TextView

String someText1 = "<font color='#FF0000'>this is red text</font>";
String someText2 = "Standard text"
yourTextView.setText(Html.fromHtml(someText1 + someText2));

This doesn't however completely answer your question as you will need to accomplish the following steps.

  1. grab the text values
  2. convert to a number and perform the conditional test above.
  3. wrap the text values in html formatting as shown above.
  4. Write back out to a string.

You could try regular expressions, or splitting the string to an array. Afraid i don't know this off the top of my head.

Community
  • 1
  • 1
Emile
  • 11,451
  • 5
  • 50
  • 63
  • Sorry, i used this example as a guide http://stackoverflow.com/questions/5865200/android-how-can-i-display-all-xml-values-of-same-tag-name , I place the data in a list and show in a textview by toString, then show the values ​​in a list but only from a textview – edwin rojas Aug 27 '12 at 21:53
  • and I think it is not very convenient, I used contains (-) but I only captures the first value if negative shows me all the values ​​that follow in red – edwin rojas Aug 27 '12 at 21:56