JAL
JAL

Reputation: 93

How can I change the color dynamically in listview base on the value from xml parse?

Here's I want to code.

Pay the girl = 100 the value of the 100 (100 is the value from XML file in the browser) = is color red

Pay the girl = 200 the value of the 200 (200 is the value from XML file in the browser) = is color green

Pay the boy = 100 the value of the 100 (100 is the value from XML file in the browser) = is color red

Pay the boy = 200 the value of the 200 (200 is the value from XML file in the browser) = is color green

Where if higher value from the XML file in the browser like 200 the color of 200 in the list-view is green while the lower value from the XML file in the browser like 100 the color of 100 in the list-view is red. the value its is change depends in the XML file in the browser. I'm using DOM parser to get the value of pay the boy and pay the girl and I'm using also Timer to auto refresh it in seconds. How can I put the code of my asking changing color in my code XML parse with timer? I do some thing in that but I'm failed.

here's the code

@Override
         public void onResume() {
          super.onResume();
          Timer autoUpdate = new Timer();
          autoUpdate.schedule(new TimerTask() {
           @Override
           public void run() {
            runOnUiThread(new Runnable() {
             public void run() {
                 ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

                 quotesXMLParsing parser = new quotesXMLParsing();
                String xml = parser.getXmlFromUrl(URL); // getting XML
                Document doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_item);
                // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(KEY_price, parser.getValue(e, KEY_price));
                    map.put(KEY_pay of the girl, parser.getValue(e, KEY_pay of the girl));
                    map.put(KEY_pay of the boy,  parser.getValue(e, KEY_pay of the boy));


                    // adding HashList to ArrayList
                    menuItems.add(map);
                }

                // Adding menuItems to ListView
                ListAdapter adapter = new SimpleAdapter(Activity.this, menuItems,
                        R.layout.showquotes,
                        new String[] { KEY_price, KEY_pay of the girl, KEY_pay ogf the boy }, new int[] {
                                R.id.price, R.id.pay of the girl, R.id.pay of the boy });

                setListAdapter(adapter);
             }
            });
           }
          }, 0, 5000); 
         }
}

here's the XML file

<items>
<item>
<price>100</price>
<pay of the girl>100</pay of the girl>
<pay of the boy>200</pay of the boy>
</item>

the value like 100 and 200 change every 5 seconds

here's I tried the code but I failed.

ListAdapter adapter = new SimpleAdapter(Activity.this, menuItems,
                        R.layout.showquotes,
                        new String[] { KEY_price, KEY_pay of the girl, KEY_pay ogf the boy }, new int[] {
                                R.id.price, R.id.pay of the girl, R.id.pay of the boy });               


            ListAdapter.setViewBinder(new MyViewBinder1());           
            itemlist.setAdapter(ListAdapter);



 class MyViewBinder1 implements ViewBinder 
 {      @Override
            public boolean setViewValue(View view, Object Status,String textRepresentation)
            {       
                  String complet="Pay of the girl:+higher ";
                  String complet1="Pay of the boy:+higher";
          String notcomplet="Pay of the girl:-lower";
                  String notcomlet="Pay of the boy:-lower";


                  String getdata= textRepresentation;


                    if((view instanceof TextView) & (Status instanceof String) )
                    { 
                            TextView iv= (TextView) view;     
                            if(complet.equals(Status))
                                {
                                r1=true;
                                r2=false;
                            iv.setText(textRepresentation);       
                            iv.setTextColor(Color.parseColor("#008000"));                
                            return true;
                           }   
                            else if(notcomlet.equals(Status))
                            {
                                r2=true;
                                r1=false;
                                iv.setText(textRepresentation);       
                                iv.setTextColor(Color.RED); 
                                return true;
                            }

                            if(r1 && (getdata.startsWith("Pay of the girl:   ")))
                           {                            

                            iv.setText(textRepresentation);      

                            iv.setTextColor(Color.parseColor("#008000"));                           
                            return true;
                           }
                if(r1 && (getdata.startsWith("Pay of the boy:   ")))
                           {                            

                            iv.setText(textRepresentation);      

                            iv.setTextColor(Color.parseColor("#008000"));                           
                            return true;
            {
                        else if (r2 && (getdata.startsWith("Pay of the girl:   ")))
                        {
                            //TextView iv= (TextView) view;
                            iv.setText(textRepresentation);       
                            iv.setTextColor(Color.RED);                              
                            return true;
                        }
            else if (r2 && (getdata.startsWith("Pay of the boy:   ")))
                        {
                            //TextView iv= (TextView) view;
                            iv.setText(textRepresentation);       
                            iv.setTextColor(Color.RED);                              
                            return true;                           
                    }        
                  return false;
            }           
}

here I want to ask how to? this images.as an reference

enter image description here

Upvotes: 2

Views: 411

Answers (1)

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

If I understood what are your requests, I can propose you to use a custom ListAdapter for your listview with one property element which is the background. Then, when you are populating the listView, for each element you should set the background (red or green).

If you would like to learn about custom ListAdapters, here is the link http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

Remember that the custom ListAdapter extends the BaseAdapter.

If you need more help about, feel free to ask me.

Upvotes: 1

Related Questions