2011-11-04 34 views
1

在我的项目中,我需要创建Listview。我创建了text.xml和imageview的row.xml以显示在列表项中。我在strings.xml文件中提到了这些列表项名称。但是我怎么能提到strings.xml文件中的图像。我怎么能这样做。 请帮我解决这个问题。如何在Android中为每个listitem放置不同的图像?

+1

添加整数阵列与图像资源ID .. –

+0

请你可以举个例子吗? – Madhuri

回答

2

拍摄图像与resourceIds数组,并设置ImageView的背景,你都将在textviews价值得到imageIds的阵列图像。您不需要将图像或任何东西放入strings.xml。试试这个link

0

在你的适配器中,你可以明确地设置图像到每个item的ImageView。你可以使用if-else if-else来设置,取决于位置,如果你有几个项目要显示每一次。您可以设置一个数组并为大量项目设置相应的图像。

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; 
     } 
    } 

} 
+0

Resources res = getResources(); TypedArray图标= res.obtainTypedArray(R.array.app1); Drawable drawable = icons.getDrawable(0);我给这样在我的java文件,但它没有检测到阵列...笏做plz帮助我 – Madhuri

+0

我检查一下这样......这对我的作品通过设置简单的图像.. ImageView的IM =(ImageView的) findViewById(R.id.imageView1); im.setImageDrawable(drawable); –

+0

嗨对不起,我创造了价值的文件夹images.xml和我已经定义了array..But现在我在arrays.xml定义和它没有显示出任何错误 – Madhuri

相关问题