【问题标题】:How to setImageResource for multiple imageViews through getview如何通过getview为多个imageViews setImageResource
【发布时间】:2012-07-27 10:44:42
【问题描述】:

这是我的自定义列表视图的行。如您所见,共有三个 imageView。我应该如何覆盖

public View getView(int position, View convertView, ViewGroup parent)

我的适配器类,这样,我可以设置 imageView.setImageResource,无论我的列表中有多少图像视图。非常感谢任何帮助。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<RelativeLayout
        android:id="@+id/LinearLayout01"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
<ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10px" />
<ImageView
        android:id="@+id/image1"
        android:layout_toRightOf= "@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10px" />

<ImageView
        android:id="@+id/image2"
        android:layout_toRightOf= "@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10px" />
</RelativeLayout>
</LinearLayout>

【问题讨论】:

  • 我想你也上过Holder-Class,现在你必须设置那个Holder-Class的图像资源。
  • 看到这个example

标签: android android-layout android-intent android-emulator


【解决方案1】:

在你的适配器中学习 holder 类

// View Holder for fast accessing List Row
private class ViewHolder {
    public ImageView image;
    public ImageView image1;
    public ImageView image2;
}

还有getView方法

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.row_layout,null);
                holder.image = (ImageView)convertView.findViewById(R.id.image);
                holder.image1 = (ImageView)convertView.findViewById(R.id.image1);
                holder.image2 = (ImageView)convertView.findViewById(R.id.image2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // Setting Image Resource here
            holder.image.setImageResource(R.drawable.icon);
            holder.image1.setImageResource(R.drawable.icon);
            holder.image2.setImageResource(R.drawable.icon);

            // return the listview row
            return convertView;
        }

在 ListView 行布局why you take LinearLayout and then RelativeLayout inside it,就take RelativeLayout as a root node.

【讨论】:

  • 谢谢;我有一个可绘制对象的列表,那么在 getView 中是否有一个循环机制的范围,我可以遍历列表并为 Imageviews 设置图像资源,即使这是以修改我当前的布局为代价的?
  • 您的列表包含可绘制对象名称或 ID ??
  • public Integer[] mThumbIds = { R.drawable.call, R.drawable.contacts, R.drawable.places, R.drawable.navigation, R.drawable.music, R.drawable.messages, };
  • 如果你想在 image,image1 & image2 中设置相同的图像,那么你可以使用 holder.image.setImageResource(mThumblds[position]);
  • 好的,稍后再试。但是我得到了这个代码的空指针:07-27 11:19:27.669: ERROR/AndroidRuntime(3908): FATAL EXCEPTION: main java.lang.NullPointerException at com.example.ImageAdapter.getView(ImageAdapter.java:73) at android.widget.AbsListView.obtainView(AbsListView.java:2093) at android.widget.ListView.makeAndAddView(ListView.java:1802)
猜你喜欢
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多