Java 如何根据xml解析中的值在listview中动态更改颜色?
这是我想要的代码 Pay the girl=100 100的值(100是浏览器中XML文件的值)=为红色 Pay the girl=200 200的值(200是浏览器中XML文件的值)=为绿色 支付男孩=100 100的值(100是浏览器中XML文件的值)=为红色 支付男孩=200 200的值(200是浏览器中XML文件的值)=为绿色 其中,如果浏览器中XML文件的值较高(如200),列表视图中的200颜色为绿色,而浏览器中XML文件的值较低(如100),列表视图中的100颜色为红色。其更改的值取决于浏览器中的XML文件。我使用DOM解析器来获取pay the boy和pay the girl的值,我还使用计时器在几秒钟内自动刷新它。如何使用计时器在代码XML解析中放入颜色更改请求的代码?我在这方面做了一些事情,但我失败了 这是密码Java 如何根据xml解析中的值在listview中动态更改颜色?,java,android,Java,Android,这是我想要的代码 Pay the girl=100 100的值(100是浏览器中XML文件的值)=为红色 Pay the girl=200 200的值(200是浏览器中XML文件的值)=为绿色 支付男孩=100 100的值(100是浏览器中XML文件的值)=为红色 支付男孩=200 200的值(200是浏览器中XML文件的值)=为绿色 其中,如果浏览器中XML文件的值较高(如200),列表视图中的200颜色为绿色,而浏览器中XML文件的值较低(如100),列表视图中的100颜色为红色。其更改的值
@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);
}
}
在这里我想问一下怎么做?这张照片是作为参考的
如果我了解您的请求是什么,我可以建议您为listview使用一个自定义ListAdapter,其中一个属性元素是背景。然后,在填充listView时,应为每个元素设置背景(红色或绿色) 如果您想了解自定义ListAdapters,请访问以下链接 请记住,自定义ListAdapter扩展了BaseAdapter
如果您在这方面需要更多帮助,请随时问我。您的问题不清楚。您可以发布您迄今为止尝试过的代码吗?这是使用时间解析xml的代码。您的xml中有一个错误的部分。100,应该是100你只能有两个价格,或者可以更多?@Milos你是说我的listview项目是什么意思?你是说我改变我的简单适配器替代自定义Listadapter?确切地说,你可以使用自己的自定义Listadapter,而不是使用Android SimpleAdapter,并改变其中的背景。第一次使用自定义适配器可能很难理解,但一旦您进入,它实际上就是Power傻瓜,您可以根据需要管理列表(添加图像、子项等)……好的,我理解您的意思,但如果我选择自定义Listadapter,那么我上面的代码是否也会更改?:)@日航,是的,你的代码会有一点变化。您不再需要MyViewBinder1类。然后,必须创建自定义适配器类、自定义适配器xml文件,并将自定义适配器设置为listview。列表数组应该有一个附加值,例如backgroundColor。有关如何操作的更多信息,请使用我提供给您的网站,或通过Ggoling custom ListAdapter android查找其他信息如果您对答案感到满意,您可以接受答案,如果您对custom list adapter有更多问题,请打开新问题并将链接放在此处,我将帮助您。。
<items>
<item>
<price>100</price>
<pay of the girl>100</pay of the girl>
<pay of the boy>200</pay of the boy>
</item>
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;
}
}