如何动态更改颜色在列表视图基础上,从XML解析的价值? [英] How can I change the color dynamically in listview base on the value from xml parse?

查看:118
本文介绍了如何动态更改颜色在列表视图基础上,从XML解析的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想code。

支付女孩= 100的100的值(100从XML文件中的浏览器的值)=是红色

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

支付女孩= 200 200的值(200从XML文件在浏览器中值)=是绿颜色

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

支付男孩= 100的100的值(100从XML文件中的浏览器的值)=是红色

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

支付男孩= 200 200的值(200从XML文件在浏览器中值)=是绿颜色

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

如果当从像200浏览器中的XML文件价值更高的200中的列表视图的颜色是绿色的,而在像100中的浏览器中的XML文件的较低值的100中的列表视图的颜色是红。它是变化的值取决于在浏览器中的XML文件英寸我使用DOM解析器来获得支付孩子的价值和支付的女孩,我也使用定时器自动刷新它在几秒钟。我怎样才能把我的要求改变颜色的code在我的code与XML解析计时器?我做的一些事情,但我失败了。

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.

这里的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); 
         }
}

这里的XML文件

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>

像100和200的变化值,每5秒

the value like 100 and 200 change every 5 seconds

这里是我试过code,但我失败了。

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;
            }           
}

在这里我想问问怎么样?这images.as一个参考

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

推荐答案

如果我明白你有什么要求,我可以建议你使用自定义ListAdapter你的列表视图与作为背景的​​一个属性元素。然后,当你填充ListView中,对于每一个元素,你应该设置背景(红色或绿色)。

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).

如果您想了解关于自定义ListAdapters,这里是链接的 http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

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

记住,定制ListAdapter扩展了BaseAdapter。

Remember that the custom ListAdapter extends the BaseAdapter.

如果您需要了解更多的帮助,请随时问我。

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

这篇关于如何动态更改颜色在列表视图基础上,从XML解析的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆