更改您的字符串數組ATLEAST這個樣子:
<string-array name="menu_array">
<item>
Page1 #ffffff
</item>
<item>
Page2 #ffffBB
</item>
<item>
Page3 #fff45f
</item>
<item>
Page4 #ffff00
</item>
</string-array>
我試圖解析您指定的字符串數組,我所獲得的形式的結果:
Page1 #ffffff
這一點,就我個人而言幾乎沒用,因爲你將不得不仍然分割使用的代碼(如列表是實際字符串數組):
String[] cmenu =list[i].split("#");
item.name=cmenu[0];
item.colour="#"+cmenu[1];
menuList.add(item);
或者你可以使用一個字符串的方法來做it.However我無法分析您安排的顏色值(總是給我一個NumberFormatException異常),而我specifed似乎是一個與做工精細:
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;
}
注:產品ColouredMenuItem的對象,它只是在一個類包裝String name,colour
我解決問題的目的創建的自定義接口的方法。
的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);
}
編輯:您還可以創建,你可以添加到資產和使用任何解析器可用解析它的XML文件。
Go to Multidimensional Array –
檢查此鏈接http://stackoverflow.com/questions/5188137/android-multi-dimensional-arrays-in-arrays-xml/5188601#5188601 –