如何在android中为每个listitem添加不同的图像?

时间:2011-11-04 08:40:03

标签: android

在我的项目中,我需要创建Listview。我已经为textview和imageview创建了row.xml以在listitems中显示。我在strings.xml文件中提到了那些列表项名称。但如何在strings.xml文件中提及图像。我该怎么做呢。 请帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

使用resourceIds获取图像数组并设置imageview的背景,在textview中插入值,从imageIds数组中获取图像。您不需要将图像或任何内容放在 strings.xml 中。试试这个link

答案 1 :(得分:0)

在适配器中,您可以将图像显式设置为每个项目的ImageView。如果每次都要显示的项目很少,您可以使用if-else if-else设置,具体取决于位置。或者您可以设置数组并相应地为大量项目设置图像。

答案 2 :(得分:0)

只需使用以下代码..

   Resources res = getResources();
   TypedArray icons = res.obtainTypedArray(R.array.icons);
   Drawable drawable = icons.getDrawable(0);//0 is index of your icons array

 <array name="icons">
        <item>@drawable/icon1</item>
        <item>@drawable/icon2</item>
        <item>@drawable/icon3</item>

    </array>

您也使用link。有类型数组的例子。

请确保图像lenth和string_array长度应该相等,因为我没有放入失败条件。

public class TestActivity extends Activity implements OnItemClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ListView list=(ListView) findViewById(R.id.listView1);
        Resources res = getResources();
        TypedArray icons = res.obtainTypedArray(R.array.icons);


        String[] str=res.getStringArray(R.array.string_array);

        list.setAdapter(new Adapter(this,icons,str));

        list.setOnItemClickListener(this);

    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) 
    {
        Toast.makeText(TestActivity.this,"You are clicked at:"+pos, Toast.LENGTH_SHORT).show();
    }
    class Adapter extends BaseAdapter
    {
        private Context context;
        private LayoutInflater inflater;
        TypedArray icons;
        String[] str;
        public Adapter(Context mContext, TypedArray icons,String[] str) 
        {
            this.context= mContext;
            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.icons=icons;
            this.str=str;
        }

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

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }
        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int pos, View convertView, ViewGroup arg2) {
            final MainListHolder mHolder;
            View v = convertView;
            if (convertView == null)
            {
                mHolder = new MainListHolder();
                v = inflater.inflate(R.layout.inflate_layout, null);
                mHolder.mTextview=(TextView) v.findViewById(R.id.textView1);
                mHolder.imageView=(ImageView) v.findViewById(R.id.imageView1);
                v.setTag(mHolder);
            } 
            else
            {
                mHolder = (MainListHolder) v.getTag();
            }

            mHolder.mTextview.setText(str[pos]);
            Drawable drawable = icons.getDrawable(pos);//0 is index of your icons array
            mHolder.imageView.setImageDrawable(drawable);//Your Drawables array
            return v;
        }
        class MainListHolder 
        {

            private ImageView imageView;
            private TextView mTextview;
        }
    }

}