-1

嗨,我想要做的是读取一个 xml 文件,该文件具有两个值颜色和名称,并显示名称并更改列表中该单个项目的背景颜色。有谁知道如何做到这一点?

这是我的xml

<?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>
4

2 回答 2

0

Change your String Array to atleast look like this:

    <string-array name="menu_array">

    <item>
      Page1 #ffffff
    </item> 

    <item>
      Page2 #ffffBB
    </item> 

    <item>
     Page3 #fff45f
    </item> 

    <item>
     Page4 #ffff00
    </item> 

    </string-array>

I have tried to parse the string-array you specified,I obtained the result of the form:

   Page1 #ffffff

This is,as far as I am concerned practically useless as you will have to still split your code using(where list is the actual string array):

        String[] cmenu =list[i].split("#");
        item.name=cmenu[0];
        item.colour="#"+cmenu[1];
        menuList.add(item);

Or you could use a subString method to do it.However I could not parse the colour value from your arrangement(always gave me a NumberFormatException) while the one I specifed seemed to work fine with:

           int Color.parseColor(String color)

This is the code of the getView method of the custom adapter I created for the purpose of solving the problem:

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

NOTE:item is an object of ColouredMenuItem which just wraps String name,colour in a class.

The customAdapter has the constructor:

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

EDIT:You could also create an XML file that you could add to assets and parse it using any of the parsers available.

于 2013-10-24T13:16:40.450 回答
0

I'm afraid you'll have to parse item values in your custom adapter or use two different arrays, one for names and another for colors

于 2013-10-23T09:40:11.593 回答