1

I created an app which displays a list of fruit and when selected the new activity display the image and the name of the fruit. 但是,我需要本地化应用程序,并且我的字符串存储在 Java 中的字符串数组中,而不是存储在 strings.xml 值文件夹中。

有没有一种简单的方法可以将 xml 字符串读入数组,如果本地设置切换为法语,它会起作用吗?(值-fr)。

package com.example.favouritefruit;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FavouriteFruit extends ListActivity 
{
    //string array containing types of fruit
      static final String[] FRUIT = new String[]
            {
                "Apple", "Orange", "Banana"
            };

      static final String SELECTEDFRUIT = "com.example.favouritefruit.SELECTEDFRUIT";

      @Override//onCreate Method
        public void onCreate(Bundle savedInstanceState) 
            {
            super.onCreate(savedInstanceState);

            /*Use a java layout as opposed to an xml layout.
            setListAdapter takes a list adapter or an array adapter
            Creates a new class ArrayAdapter Class*/
            setListAdapter(new ListArrayAdapter(this, FRUIT));
            }

        //Creates an intent to move to a new activity

      public void onListItemClick(ListView l, View v, int position, long id)
      {
          super.onListItemClick(l, v, position, id);


        try {
            Class test = Class.forName("com.example.favouritefruit.AppleActivity");
            Intent intent = new Intent(FavouriteFruit.this, test);

            TextView textView = (TextView) v.findViewById(R.id.label);
            String fruit = textView.getText().toString();
            ImageView fruitImage = (ImageView)v.findViewById(R.id.logo);

            //intent.putExtra("IMAGE",""+);
            intent.putExtra(SELECTEDFRUIT,fruit);
            startActivity(intent);
            }   catch (ClassNotFoundException e) {
            e.printStackTrace();}

      }

}

class ListArrayAdapter extends ArrayAdapter<String> 
{
        //Context allows the retrieval of resources such as layout
        private final Context context;
        private final String[] fruit;

        //create the ArrayAdpater
        public ListArrayAdapter(Context context, String[] fruit) 
        {
        super(context, R.layout.activity_favourite_fruit, fruit);
        this.context = context;
        this.fruit = fruit;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //inflater  dynamically loads the layouts
        View rowView = inflater.inflate(R.layout.activity_favourite_fruit, parent, false);
        //get the textView
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        //get the ImageView
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        //set the text to the string values based on position
        textView.setText(fruit[position]);

        // Change item based on its position in the string array
        String fruitPosition = fruit[position];

        System.out.println(fruitPosition); 
        //assign the image to to the relevant fruit
        if (fruitPosition.equals("Apple"))
            {
            imageView.setImageResource(R.drawable.image_apple);
            } 
        else if (fruitPosition.equals("Orange")) 
            {
            imageView.setImageResource(R.drawable.image_apple);
            } 
        else if (fruitPosition.equals("Banana"))
            {
            imageView.setImageResource(R.drawable.image_apple);
            }

        //return the layout
        return rowView;
        }

}
4

1 回答 1

1

要从资源中获取 xml 字符串数组,请使用:

getResources().getStringArray(R.array.string_array_name);

是的,如果您有资源的本地化版本,它将根据用户的设备设置自动加载适当的版本。您可以制作单个字符串的本地化 xml 资源,并使用通过 id/name 引用字符串资源的单个字符串数组资源

于 2012-10-16T16:31:28.727 回答