如何使用Drawable图像名称</string>从ArrayList <string>动态设置ImageViews源

时间:2014-01-13 11:20:58

标签: android listview imageview drawable

我有一个ListView,每行都有一个ImageView,一个TextView和另一个ImageView。

下面的代码是我的ListView行的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FFF">
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@drawable/ic_default"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:textSize="16sp" />
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/ic_cena_play"
android:layout_marginRight="0dp"
android:layout_marginLeft="10dp"
android:clickable="true"
android:background="@drawable/mybuttonplay" />
</LinearLayout>

我有一个ArrayList&lt;字符串&gt;使用Drawables的名称,我想放在行的第一个ImageView中。我想将ImageViews源“ic_default”更改为在ArrayList中具有名称的源名称。

在我的Activity中,在onCreate()中,我正在使用一个Adapter,如下面的代码所示:

ArrayList<String> items = new ArrayList();
items.add("Star");
items.add("Moon");
items.add("Dog");
items.add("Bird");
items.add("Tree");
ArrayList<String> itemsimg = new ArrayList();
itemsimg.add("ic_star");
itemsimg.add("ic_moon");
itemsimg.add("ic_dog");
itemsimg.add("ic_bird");
itemsimg.add("ic_tree");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row =  super.getView(position, convertView, parent);
            ImageView left = (ImageView) row.findViewById(R.id.left);
            //here I want to change the resource
            //Like if I had a function called getRosourceId(). DOES IT EXIST?!
            int x = getResourceId( itemsimg.get(position) );
            left.setImageResource(x);
            View right = row.findViewById(R.id.right);
            right.setTag(position);
            right.setOnClickListener(ScenesActivity.this);
            return row;
        }
    };

更新

谢谢你的回答!现在我可以改变ImageView源的语音(但尚未从ArrayList&lt; String&gt;)。之前没有setImageResource()方法,因为我有一个View。随着你对所有人的建议施展到ImageView,大部分问题都没问题。但答案并没有涵盖“资源名称”的恐怖之处。

现在的最后一个问题是left.setImageResource(resId);方法从Resource请求一个ID,我有一个String。怎么做?

抱歉,其他代码版本有一些错误。现在它已修复。我认为现在最后一个问题很清楚了。

4 个答案:

答案 0 :(得分:13)

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View row =  super.getView(position, convertView, parent);
        ImageView left = (ImageView)row.findViewById(R.id.left);
        left.setOnClickListener(ScenesActivity.this);
        ImageView right = (ImageView)row.findViewById(R.id.right);
        right.setTag(position);

       right.setImageResource(itemsimg.get(position));
       left.setImageResource(itemsimg.get(position));

       right.setOnClickListener(ScenesActivity.this);
       return row;
    }

使用以下内容更改itemsimg:

ArrayList<Integer> itemsimg = new ArrayList<Integer>();
itemsimg.add(R.drawable.ic_star);
itemsimg.add(R.drawable.ic_moon);
itemsimg.add(R.drawable.ic_dog);
itemsimg.add(R.drawable.ic_bird);
itemsimg.add(R.drawable.ic_tree);

使用String获取drawableId可以执行此操作:

Context context = imageView.getContext();
int id = context.getResources().getIdentifier("ic_tree", "drawable", context.getPackageName());
imageView.setImageResource(id);

效率不高,但它可以查找偶尔的资源。

答案 1 :(得分:3)

试试这个::

right.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

左边同样

left.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

讨论后

       ArrayList<String> items = new ArrayList();
    items.add("ic_star");
    items.add("ic_moon");
    items.add("ic_dog");
    items.add("ic_bird");
    items.add("ic_tree");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row =  super.getView(position, convertView, parent);
/*View : not required to declare everytimes*/
                ImageView left = row.findViewById(R.id.left);
                left.setOnClickListener(ScenesActivity.this);
                ImageView right = row.findViewById(R.id.right);
                right.setTag(position);
                right.setOnClickListener(ScenesActivity.this);
                right.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/
                left.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

                return row;
            }
        };

答案 2 :(得分:2)

替换你的代码:

ArrayList<String> items = new ArrayList();
items.add("ic_star");
items.add("ic_moon");
items.add("ic_dog");
items.add("ic_bird");
items.add("ic_tree");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row =  super.getView(position, convertView, parent);
            ImageView left = (ImageView) row.findViewById(R.id.left);
            left.setOnClickListener(ScenesActivity.this);
            left.setImageResource(R.drawable.leftphoto);
            ImageView right = (ImageView) row.findViewById(R.id.right);
            right.setTag(position);
            right.setOnClickListener(ScenesActivity.this);
            right.setImageResource(R.drawable.rightphoto);
            return row;
        }
    };

答案 3 :(得分:0)

另一种简单而有条理的方法:

1)在 drawable 文件夹中添加资源,假设(ic_home,ic_launcher,ic_logout)//可以添加任意数量的图像

2)在values文件夹中的 strings.xml 中添加这些名称以及将在图像旁边显示的名称。它看起来像这样......

 <!-- Drawer List Items -->
<string-array name="nav_drawer_items">
    <item>Home</item>
    <item>Add Category</item>
    <item>Logout</item>
</string-array>
<string-array name="nav_drawer_icons">
    <item>ic_home</item>
    <item>ic_addcategory</item>
    <item>ic_logout</item>
</string-array>

3)现在创建一个pojo(getter / setter)类,它将在对象中保存图标文本和图标id。像这样......

public class DrawerItems {

private String icnName;
private int icnId;

/**
 * @return the icnName
 */
public String getIcnName() {
    return icnName;
}

/**
 * @param icnName
 *            the icnName to set
 */
public void setIcnName(String icnName) {
    this.icnName = icnName;
}

/**
 * @return the imageid
 */
public int getIcnId() {
    return icnId;
}

/**
 * @param imageid
 *            the imageid to set
 */
public void setIcnId(int imageid) {
    icnId = imageid;
}}

4)现在我们需要在将它提供给适配器之前填充它。我们可以这样做

private DrawerItems navigationDrawerItems;
private ArrayList<DrawerItems> dataObjects;
private String[] mDrawerListItems;
private String[] mDrawerIcons;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // to fetch the resources from the string files
    mDrawerListItems = getResources().getStringArray(
            R.array.nav_drawer_items);
    mDrawerIcons = getResources().getStringArray(R.array.nav_drawer_items);

    // drawerListAdapter = new DrawerListAdapter(this, mDrawerListItems);
    dataObjects = new ArrayList<DrawerItems>();
    for (int i = 0; i < mDrawerListItems.length; i++) {
        navigationDrawerItems = new DrawerItems();
        navigationDrawerItems.setIcnName(mDrawerListItems[i]);
        navigationDrawerItems.setIcnId(getResources().getIdentifier(
                mDrawerIcons[i], "drawable", this.getPackageName()));
        dataObjects.add(navigationDrawerItems);
    }

    drawerListAdapter = new DrawerListAdapter(this,
            R.layout.list_drawer_row_item, dataObjects);//add name of your layout
}

5)现在在适配器中你可以这样实现它

public class DrawerListAdapter extends ArrayAdapter {

private Context mContext;
private ArrayList<DrawerItems> mDataObjects;
private int layout;

public DrawerListAdapter(Context context, int resource,
        ArrayList<DrawerItems> dataObjects) {
    super(context, resource, dataObjects);
    mContext = context;
    mDataObjects = dataObjects;
    layout = resource;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(layout, parent,
                false);

        holder.txtDrawerListItem = (TextView) convertView
                .findViewById(R.id.txt_drawer_item_name);
        holder.imgDrawerListIcon = (ImageView) convertView
                .findViewById(R.id.img_drawer_item_icon);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final DrawerItems dataObj = mDataObjects.get(position);
    holder.txtDrawerListItem.setText(dataObj.getIcnName());
    holder.imgDrawerListIcon.setImageResource(dataObj.getIcnId());

    return convertView;
}

public static class ViewHolder {
    TextView txtDrawerListItem;
    ImageView imgDrawerListIcon;
}
}

6)你已经完成了......确保你已经初始化了视图和常见的东西