如何在Android中使用xml使用资源数组?

时间:2013-05-17 19:24:58

标签: android xml arrays resources

我是Android开发的新手,面临管理Android资源的问题。我想用ImageView和TextView创建一个listView。

以下是我的实现工作正常,但实际上我想使用我之前创建的数组:

int[] img = getResources().getIntArray(R.Array.img);
package com.simplelistviewwithlistactivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

public class ListActivityS extends ListActivity {
    int[] img = { R.drawable.r1, R.drawable.r2, R.drawable.skycubemap1,
            R.drawable.skycubemap1, R.drawable.skycubemap2,
            R.drawable.skycubemap3, R.drawable.skycubemap4,
            R.drawable.skycubemap5 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setDividerHeight(2);
        getListView().setAdapter(new BindDataAdapter(this, img, item));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(item[position] + " is clicked.");
        builder.setPositiveButton("OK", null);
        builder.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list, menu);
        return true;
    }

    private String item[] = { "This is list Item1", "This is list Item2",
            "This is list Item3", "This is list Item4", "This is list Item5",
            "This is list Item6", "This is list Item8", "This is list Item8"

2 个答案:

答案 0 :(得分:31)

创建如下所示的XML并将其放在res / values / arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

然后使用这样的代码:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

来源:http://developer.android.com/guide/topics/resources/more-resources.html

答案 1 :(得分:5)

您可以使用void compute_a(int al, int au, int bl, int bu, int cl, int cu, int dl, int du, float *var, float *cv, float *sdi, float *sdeg, int *cc) { #pragma omp parallel for for(int a=al; a<=au; a++) { int up[n][5]; int down[n][5]; int up_i=0; int down_i=0; for(int b=bl; b<=bu; b++) { for(int c=cl; c<=cu; c++) { for(int d=dl; d<=du; d++) { float result[n]; for(int i=0; i<n; i++) { result[i]=a*(1-var[i])+b*(1-cv[i])+c*sdi[i]+d*sdeg[i]; } int ccc[n] for(int kk=0; kk<n; kk++) ccc[kk]=cc[kk]; quickSortd2(result, ccc, 0, n-1); int count_of_ones=0; for(int ii=0; ii<1000; ii++) if(ccc[ii]==1) count_of_ones++; if(count_of_ones>150) { up[up_i][0]=a; up[up_i][1]=b; up[up_i][2]=c; up[up_i][3]=d; up[up_i++][4]=count_of_ones; } else if(count_of_ones<121) { down[down_i][0]=a; down[down_i][1]=b; down[down_i][2]=c; down[down_i][3]=d; down[down_i++][4]=count_of_ones; } } } } FILE *fpup; FILE *fpdown; char* path_up_p1="ResultUp150forA ("; char* path_up_p2=").csv"; char* path_down_p1="ResultDn121forA ("; char* path_down_p2=").csv"; char *path_up; char *path_down; char str_file_number[5]; itoa(a, str_file_number, 10); path_up=(char*)malloc(strlen(path_up_p1)+strlen(str_file_number)+strlen(path_up_p2)+1); path_up[0]='\0'; strcat(path_up,path_up_p1); strcat(path_up,str_file_number); strcat(path_up,path_up_p2); fpup=fopen(path_up, "w"); for(int t=0; t<up_i; t++) fprintf(fpup, "%d,%d,%d,%d,%d\n", up[t][0], up[t][1], up[t][2], up[t][3], up[t][4]); fclose(fpup); path_down=(char*)malloc(strlen(path_down_p1)+strlen(str_file_number)+strlen(path_down_p2)+1); path_down[0]='\0'; strcat(path_down,path_down_p1); strcat(path_down,str_file_number); strcat(path_down,path_down_p2); fpdown=fopen(path_down, "w"); for(int t=0; t<up_i; t++) fprintf(fpdown, "%d,%d,%d,%d,%d\n", down[t][0], down[t][1], down[t][2], down[t][3], down[t][4]); fclose(fpdown); } } 中的资源。

对于drawables

res/values/arrays.xml

对于colors

<integer-array name="your_images">
    <item>@drawable/ic_active_image</item>
    <item>@drawable/ic_visited_image</item>
</integer-array>

val drawables = resources.obtainTypedArray(R.array.your_images)
val drawable = drawables.getResourceId(position, -1)
image.setImageResource(drawable)
drawables.recycle()

对于strings

<array name="your_colors">
    <item>#365374</item>
    <item>#00B9FF</item>
</array>

val colors = resources.obtainTypedArray(R.array.your_colors)
val color = colors.getColor(position, -1)
title.setTextColor(color)
colors.recycle()

Plurals

<string-array name="your_strings">
    <item>Active</item>
    <item>Visited</item>
</string-array>

val strings = resources.getStringArray(R.array.your_strings)
title.text = strings[position]