java - 如何根据 xml 解析的值在 listview 中动态更改颜色?

标签 java android

这是我要编码的地方。

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

付给女孩 = 200 200 的值(200 是来自浏览器中 XML 文件的值)= 颜色为绿色

付给男孩 = 100 100 的值(100 是来自浏览器中 XML 文件的值)= 是红色

支付给男孩 = 200 200 的值(200 是来自浏览器中 XML 文件的值)= 颜色为绿色

如果来自浏览器中 XML 文件的较高值,如 200,则 ListView 中 200 的颜色为绿色,而来自浏览器中 XML 文件的较低值,如 100,则 ListView 中 100 的颜色为红色的。它的变化值取决于浏览器中的 XML 文件。我正在使用 DOM 解析器来获取付钱给男孩和付钱给女孩的值(value),我还使用 Timer 在几秒钟内自动刷新它。如何将请求更改颜色的代码放入带有计时器的代码 XML 解析中?我在这方面做了一些事情,但我失败了。

这是代码

@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 文件

<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 秒变化一次

这是我尝试过的代码,但我失败了。

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

这里我想问一下怎么办?此图像。作为引用

enter image description here

最佳答案

如果我了解您的要求,我可以建议您为您的 ListView 使用自定义 ListAdapter,其中一个属性元素是背景。然后,当您填充 listView 时,您应该为每个元素设置背景(红色或绿色)。

如果您想了解自定义 ListAdapter,请点击此处链接 http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

请记住,自定义 ListAdapter 扩展了 BaseAdapter。

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

关于java - 如何根据 xml 解析的值在 listview 中动态更改颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508092/

相关文章:

Android位图坐标奇怪

android - 抽屉导航中的 ExpandableListview

java - JOptionPane 的 setSize

java - 如何使用 jackson 序列化 java bean 并自定义映射键名称?

android - 有没有一种方法可以在 iOS 设备上显示 Android PhotoSphere?

android - Kotlin - 如何在 Telegram 应用程序中打开 Telegram 群组的链接

java - Android 中的 SimpleDateFormat 在 Android 1.6 中提供不需要的时区

java - Spring Integration——聚合分散的消息

用于奇数图像类型的 Java JPEG 转换器

java - 从服务获取 SharedPreferences