Android从资源XML填充Listview
嗨,我想做的是读取一个xml文件,它有两个值color和name,显示名称并更改列表中单个项目的背景颜色。有人知道怎么做吗 这是我的xmlAndroid从资源XML填充Listview,android,xml,Android,Xml,嗨,我想做的是读取一个xml文件,它有两个值color和name,显示名称并更改列表中单个项目的背景颜色。有人知道怎么做吗 这是我的xml <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="menu_array"> <item> <name>Page1</name> <colou
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="menu_array">
<item>
<name>Page1</name>
<colour>#ffffff</colour>
</item>
<item>
<name>Page2</name>
<colour>#ffffBB</colour>
</item>
<item>
<name>Page3</name>
<colour>#fff45f</colour>
</item>
<item>
<name>Page4</name>
<colour>#ffff00</colour>
</item>
</string-array>
</resources>
第1页
#ffffff
第2页
#ffffBB
第3页
#fff45f
第4页
#ffff00
恐怕您必须在自定义适配器中解析项
值,或者使用两个不同的数组,一个用于名称,另一个用于颜色将字符串数组更改为至少如下所示:
<string-array name="menu_array">
<item>
Page1 #ffffff
</item>
<item>
Page2 #ffffBB
</item>
<item>
Page3 #fff45f
</item>
<item>
Page4 #ffff00
</item>
</string-array>
就我而言,这实际上毫无用处,因为您仍然需要使用(其中列表是实际的字符串数组)拆分代码:
或者您可以使用子字符串方法来执行此操作。但是,我无法解析您安排的颜色值(总是给我一个NumberFormatException),而我指定的颜色值似乎可以正常使用:
int Color.parseColor(String color)
这是我为解决此问题而创建的自定义适配器的getView方法的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
TextView textView;
if(row==null)
{
row=inflater.inflate(resourceId, parent, false);
textView=(TextView)row.findViewById(R.id.text1);
row.setTag(R.id.text1,textView);
}
else
textView=(TextView)row.getTag(R.id.text1);
textView.setText(menuList.get(position).name);
try{
Log.d(TAG, menuList.get(position).colour);
row.setBackgroundColor(Color.parseColor(menuList.get(position).colour));
}
catch(Exception ex)
{
Log.e(TAG, "Still does not work");
}
return row;
}
注意:item是coloredmenuItem的一个对象,它只是将字符串名称、颜色
包装在一个类中
customAdapter具有构造函数:
MadAdapter(Context context,List<ColouredMenuItem> list,int resourceId)
{
this.context=context;
this.menuList=list;
this.resourceId=resourceId;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
madapter(上下文、列表、int-resourceId)
{
this.context=context;
this.menuList=list;
this.resourceId=resourceId;
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
}
编辑:您还可以创建一个XML文件,将其添加到资产中,并使用任何可用的解析器对其进行解析。转到多维数组检查此链接
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
TextView textView;
if(row==null)
{
row=inflater.inflate(resourceId, parent, false);
textView=(TextView)row.findViewById(R.id.text1);
row.setTag(R.id.text1,textView);
}
else
textView=(TextView)row.getTag(R.id.text1);
textView.setText(menuList.get(position).name);
try{
Log.d(TAG, menuList.get(position).colour);
row.setBackgroundColor(Color.parseColor(menuList.get(position).colour));
}
catch(Exception ex)
{
Log.e(TAG, "Still does not work");
}
return row;
}
MadAdapter(Context context,List<ColouredMenuItem> list,int resourceId)
{
this.context=context;
this.menuList=list;
this.resourceId=resourceId;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}