0

我试图在我的列表视图中添加图像,但我收到了这个错误:

You must supply a resource ID for a TextView

这是菜单布局,menu.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sahovnica"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" >

<ImageView
    android:id="@+id/imageView_Menu_Title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/vh_title" />

<ListView
    android:id="@+id/listView_Menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/imageView_Menu_Title" >

</ListView>

这是 list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView_List_Item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

这是我的代码:

public class MenuActivity extends Activity {

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

    ListView list = (ListView)findViewById(R.id.listView_Menu);

     Bitmap[] images = { decodeImage(R.drawable.igraj), 
                             decodeImage(R.drawable.rezultati),
             decodeImage(R.drawable.postavke),
             decodeImage(R.drawable.pomoc), decodeImage(R.drawable.izlaz)};

     ArrayAdapter<Bitmap> adapter = new ArrayAdapter<Bitmap>(this, R.layout.list_item,
                                                                     images);

     list.setAdapter(adapter);

}

private Bitmap decodeImage(int res) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),res);               
    return bitmap;      
}

}

我不知道这意味着什么(错误),因为我试图在 ArrayAdapter 中放入一些字符串,并在 list_item.xml 中放入一个 TextView 作为项目,但它仍然没有用,但我不知道为什么。提前致谢。

4

3 回答 3

0

ArrayAdapter文档

默认情况下,此类期望提供的资源 id 引用单个 TextView。如果您想使用更复杂的布局,请使用也采用字段 id 的构造函数。该字段 id 应引用较大布局资源中的 TextView。

[...]

要使用除 TextViews 之外的其他内容来显示数组,例如 ImageViews,或者要让 toString() 结果之外的一些数据填充视图,请覆盖 getView(int, View, ViewGroup) 以返回所需的视图类型。

您需要创建自己的BaseAdapter实现,或者创建自己的ArrayAdapter子类并覆盖getView()

使用图像的简单getView()可能如下所示:

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    row = inflater.inflateR.layout.list_item, parent, false);

    return row;
}
于 2013-10-18T16:36:46.187 回答
0

从以下文档ArrayAdapter

A concrete BaseAdapter .......to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

如您所见,您需要在TextViewR.layout.list_item 引用的布局文件中包含一个。你只有一个ImageView在那个布局中。

对于更复杂的视图(不限于TextView's,我建议您使用自己的BaseAdapter.

于 2013-10-18T16:36:54.763 回答
0

查看 ArrayAdapter 的构造函数

http://developer.android.com/reference/android/widget/ArrayAdapter.html

正如其他人建议的那样,使用 CustomAdapter

public class MenuActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    ListView list = (ListView)findViewById(R.id.listView_Menu);
    String[] a ={"hi","bye"};
    Bitmap[] images = { decodeImage(R.drawable.igraj), 
                         decodeImage(R.drawable.rezultati),
                         decodeImage(R.drawable.postavke),
                         decodeImage(R.drawable.pomoc),              
                         decodeImage(R.drawable.izlaz)
                       };
     CustomAdapter adapter = new CustomAdapter(images);
     list.setAdapter(adapter);

}

private Bitmap decodeImage(int res) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),res);               
    return bitmap;      
}
class CustomAdapter extends BaseAdapter
{
    Bitmap images[]; 
    LayoutInflater inflater;
    public CustomAdapter(Bitmap[] Images)
    {
        images=Images;
        inflater= LayoutInflater.from(MenuActivity.this);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images.length;
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder.iv= (ImageView) convertView.findViewById(R.id.imageView_List_Item);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.iv.setImageBitmap(images[position]);
        return convertView;
    }

}
class ViewHolder
{
    ImageView iv;
}
}
于 2013-10-18T17:20:45.190 回答