从ListView中的String.xml获取String数组

时间:2016-03-16 04:33:09

标签: java android arrays xml string

我正致力于通过此网站下载图片以及来自ListView的网络和延迟加载 - http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

但是,我想在listview上自定义textview。我希望我的textview从string.xml中的字符串数组中提取字符串,而不是代码holder.text.setTexr("Title " + position)

请帮助我,因为我是初学者。我很感激。谢谢。

XML:

 <string-array name="titles">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
</string-array>

这是我的主要活动

public class IngredientCategoryMain extends Activity {

ListView list;
CategoryImageAdapter adapter;
ArrayAdapter arrayAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ingredient_category_main);

    list=(ListView)findViewById(R.id.listView);
    adapter=new CategoryImageAdapter(this, mStrings);
    list.setAdapter(adapter);

}

@Override
public void onDestroy() {
    list.setAdapter(null);
    super.onDestroy();
}

public View.OnClickListener listener=new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();


    }
};

public void onItemClick(int mPosition) {
    String tempValues = mTitles[mPosition];
    Toast.makeText(IngredientCategoryMain.this, tempValues, Toast.LENGTH_LONG).show();
}

private String[] mStrings={
        "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Ic_cake_48px.svg/2000px-Ic_cake_48px.svg.png",
        "https://pixabay.com/static/uploads/photo/2013/04/01/21/30/can-99137_960_720.png",
        "http://publicdomainvectors.org/photos/Gerald_G_Fast_Food_Drinks_(FF_Menu)_9.png",
        "https://pixabay.com/static/uploads/photo/2014/03/25/16/59/apple-297775_960_720.png",
        "https://pixabay.com/static/uploads/photo/2012/04/16/11/14/mortar-35544_960_720.png",
        "https://pixabay.com/static/uploads/photo/2013/07/13/10/05/cattle-156498_960_720.png",
        "https://pixabay.com/static/uploads/photo/2013/07/12/15/39/acorn-150258_960_720.png",
        "http://publicdomainvectors.org/photos/johnny_automatic_bread_with_knife.png",
        "https://pixabay.com/static/uploads/photo/2015/09/13/00/12/chicken-937584_960_720.jpg",
        "http://publicdomainvectors.org/photos/bowl-of-steaming-soup-01.png",
        "https://pixabay.com/static/uploads/photo/2014/04/02/10/38/fish-304097_960_720.png",
        "http://publicdomainvectors.org/photos/Erbsen-lineart.png"
};

适配器:

public class CategoryImageAdapter extends BaseAdapter implements OnClickListener {

private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

public CategoryImageAdapter(Activity a, String[] d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());

}


@Override
public int getCount() {
    return data.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public void onClick(View v) {

}

public static class ViewHolder {
    public TextView text;
    public ImageButton imageButton;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView == null){
        vi = inflater.inflate(R.layout.ingcategoryrow, null);
        holder = new ViewHolder();
        holder.text = (TextView)vi.findViewById(R.id.textView2);
        holder.imageButton=(ImageButton)vi.findViewById(R.id.imageButton2);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText("Title " + position);
    ImageButton imageButton = holder.imageButton;
    imageLoader.DisplayImage(data[position], imageButton);
    vi.setOnClickListener(new OnItemClickListener(position));
    return vi;
}

private class OnItemClickListener implements OnClickListener{
    private int mPosition;

    OnItemClickListener(int position) {
        mPosition = position;
    }


    @Override
    public void onClick(View arg0) {
        IngredientCategoryMain sct = (IngredientCategoryMain)activity;
        sct.onItemClick(mPosition);

    }
}

}

2 个答案:

答案 0 :(得分:0)

首先将数组作为

String[] mTestArray = getResources().getStringArray(R.array.titles);    

然后将其用作

holder.text.setText(mTestArray[position] + position);

在构造函数中获取数组,如:

public CategoryImageAdapter(Activity a, String[] d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
    mTestArray = getResources().getStringArray(R.array.testArray);   
}

答案 1 :(得分:0)

这是我的代码,请使用此代码:

String.xml

 <string-array name="titles">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
        <item>9</item>
        <item>10</item>
        <item>11</item>
        <item>12</item>
    </string-array>

.java文件

public class MainActivity extends Activity {

    ListView lv;
    String[] title;
    CategoryImageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView) findViewById(R.id.listView1);

        title=getResources().getStringArray(R.array.titles);

        adapter=new CategoryImageAdapter(MainActivity.this, title);
        lv.setAdapter(adapter);

    }

    public class CategoryImageAdapter extends BaseAdapter implements OnClickListener {

        private Activity activity;
        private String[] data;
        private LayoutInflater inflater = null;

        public CategoryImageAdapter(Activity a, String[] d) {
            activity = a;
            data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }


        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public void onClick(View v) {

        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;
            if(convertView == null){
                vi = inflater.inflate(R.layout.row_lv, null);
                holder = new ViewHolder();
                holder.text = (TextView)vi.findViewById(R.id.textView1);
                vi.setTag(holder);
            }
            else
                holder=(ViewHolder)vi.getTag();

            holder.text.setText("Title " + data[position]);

            return vi;
        }
    }
        public class ViewHolder {
            public TextView text;

        }

}