使用R.array的变量值

时间:2013-02-06 18:52:36

标签: android xml

我有一个列表视图的活动。在单击列表视图的项目时,会将意图发送到包含所单击项目的值的下一个列表。该列表是使用xml文件生成的。

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Selected item
    String cat  = ((TextView) view).getText().toString();

    // Launching new Activity on selecting single Item List
    Intent intent   = new Intent(getApplicationContext(), ContentListing.class);

    // Sending data to new activity
    intent.putExtra("cat", cat);
    startActivity(intent);
}

在下一个活动中,我再次必须阅读xml文件,但此文件将根据上一个列表中单击的项目而有所不同。

// Creating a handle to capture data sent from previous activity
Intent intent = getIntent();

// Storing the category into a variable
String cat = intent.getStringExtra("cat");

// Storing string resources into Array
String[] itemList   = getResources().getStringArray(R.array.itemList);

我想做 String[] itemList = getResources().getStringArray(R.array.cat); 之类的事情,即 R.array.variable ,这对我不起作用。我是java和android的新手,所以欢迎任何形式的帮助(易于理解和实现)。

另外,我希望每次关于单击的项目,第二个活动的名称都不同。我应该为此做些什么?

修改

这是我更新的代码,它提供了有关getContext()

的错误
    public class ContentListing extends ListActivity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_listing);

        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true); 
        }

        // Creating a handle to capture data sent from previous activity
        Intent intent = getIntent();

        // Storing the category into a variable
        String cat = intent.getStringExtra("cat");

        setTitle(cat);

        // Line that shows error
        int resourceId = Resources.getSystem().getIdentifier(cat, "array", getContext().getPackageName());

        Log.d("Print message1: ", String.valueOf(resourceId)+"\n");
        if(resourceId != 0) {
            Log.d("Print message: ", String.valueOf(resourceId)+"\n");

            // Storing string resources into Array
            //String[] itemList = getResources().getStringArray(R.array.itemList);
            String[] itemList   = getResources().getStringArray(resourceId);

            // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, itemList));

            ListView lv = getListView();

            // Listening to single list item on click
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // Selected item
                    String product  = ((TextView) view).getText().toString();

                    // Launching new Activity on selecting single Item List
                    Intent intent   = new Intent(getApplicationContext(), ContentListing.class);

                    // Sending data to new activity
                    intent.putExtra("product", product);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_content_listing, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用变量从以下资源加载数组:

int resourceId=Resources.getSystem().getIdentifier(cat, "array", getContext().getPackageName());
if(resourceId != 0){
  getResources().getStringArray(resourceId);
}

设置活动的标题,

setTitle(cat);

但我会考虑,检查传入的额外值,然后使用if else语句加载资源。你可以更好地保证事情能够正常运作。

int resId = R.array.defaultValue;
if(cat.equals("category1"){
  resId = R.array.categoryOneValues;
} else if(cat.equals("category2"){
  resId = R.array.categoryTwoValues;
}
getResources().getStringArray(resId);