ListActivty的BaseAdapter.getView()使用ViewHolder - Android

时间:2014-01-20 07:52:10

标签: android listview baseadapter

这是我对ListView BaseAdapter的原始getView方法:

    // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            ImageHelper ih = new ImageHelper();



imageView.setImageBitmap(ih.decodeSampledBitmapFromResource(mContext.getResources(), mList.get(position), 40, 40));

            return imageView;
        }

它没有使用convertView,我没有mInflater。我读到这种方式不利于内存使用,我发现了一种使用ViewHolder的更好方法。我已经改变了这个方法来实现一个viewHolder并使用convertView代替我最初做的ImageView:

static class ViewHolder{
        TextView text;
        ImageView icon;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
              ViewHolder holder;
              ImageView imageView = new ImageView(mContext);
              ImageHelper ih = new ImageHelper();
              if (convertView == null) {
              convertView = mInflater.inflate(R.id.gridviewViewOutfits, null);

              holder = new ViewHolder();
              holder.icon = (ImageView) convertView;
              holder.icon.setLayoutParams(new GridView.LayoutParams(150, 150));
              holder.icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
              holder.icon.setPadding(8, 8, 8, 8);

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

              holder.icon.setImageBitmap(ih.decodeSampledBitmapFromResource(mContext.getResources(), mList.get(position), 40, 40));

              return convertView;
              }

此行导致nullPointerException:convertView = mInflater.inflate(R.id.gridviewViewObjects, null);

这是因为我没有实例化我的mInflater。然后我这样做:mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

因为我在网上看到了。然后我收到了这个错误:

01-20 20:37:52.080: E/AndroidRuntime(22543): FATAL EXCEPTION: main
01-20 20:37:52.080: E/AndroidRuntime(22543): android.content.res.Resources$NotFoundException: Resource ID #0x7f04002b type #0x12 is not valid
01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2136)
01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.content.res.Resources.getLayout(Resources.java:865)
01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-20 20:37:52.080: E/AndroidRuntime(22543):    at com.btf271.imagehelper.ChooseStickersImageAdapter.getView(ChooseObjectsImageAdapter.java:136)

我应该怎么做才能实例化mInflater,或者我是在冒充气?

这是我的ListActivity 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: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=".ViewOutfitsActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

    <GridView
        android:id="@+id/gridviewViewObjects"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />

</RelativeLayout>

谢谢。

4 个答案:

答案 0 :(得分:1)

当你在那时夸大了视图时,你应该使用布局

所以从这个改变

 convertView = mInflater.inflate(R.id.gridviewViewOutfits, null);

  convertView = mInflater.inflate(R.layout.gridviewViewOutfits, null);

答案 1 :(得分:0)

更改mInflater.inflate(R.id.gridviewViewOutfits,null);到mInflater.inflate(R.layout.gridviewViewOutfits,null);

答案 2 :(得分:0)

R.id.gridviewViewOutfits指的是您要填充的GridView,这是没有意义的。 您必须为GridView中要包含的项目创建布局,例如,从包含TextView的简单布局开始。然后用

膨胀该布局
mInflater.inflate(R.layout.exampleItem, null);

并用您的数据填充它。

答案 3 :(得分:0)

您需要为List项创建另一个布局文件并对其进行充气。