2 sachinks SachinKS 于 2013.07.09 10:40 提问

开发时遇到了addview错误

setAdapter()命令报错: "addview is not supported in adapterview"。源码如下:

FilmAdapter.java

package com.pabeda.bicaps.adapters;
import java.util.List;
import com.pabeda.bicaps.R;
import com.pabeda.bicaps.classes.Film;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class FilmAdapter extends ArrayAdapter<Film> {
    Context context;
    int layoutResourceId;
    Film data[]=null;       
    public FilmAdapter(Context context, int resource,Film[] objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub

        this.layoutResourceId=layoutResourceId;
        this.context=context;
        this.data=objects;      
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;   
        if (row==null) {
            LayoutInflater inflater=((Activity)context).getLayoutInflater();
            inflater.inflate(R.layout.film_list,null);
        }    
        ((TextView)row.findViewById(R.id.film_name)).setText(data[position].filmName);
        ((TextView)row.findViewById(R.id.imdb_rating)).setText(data[position].imdbRating);
        return row;    
    }
}

设置adapter如下:

FilmAdapter filmadapter=new FilmAdapter(this, R.layout.film_list, film);
GridView gw=(GridView) findViewById(R.id.gridView1);
gw.setAdapter(filmadapter);

res > Layout > film_list.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=".MainActivity" >
    <TextView
        android:id="@+id/film_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="16dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Film İsmi Buraya Gelecek"
        android:textSize="22sp" />
    <ImageView
        android:id="@+id/film_image"
        android:layout_width="85dp"
        android:layout_height="125dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:text="IMDB Rating" />
    <TextView
        android:id="@+id/imdb_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="4.9/5" />
</RelativeLayout>

1个回答

cj6585256
cj6585256   2013.07.09 11:15
已采纳

这里写法有问题,View row=convertView; convertView给了引用就得给引用赋值,即row = inflater.inflate(R.layout.film_list,null);

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row=convertView;   
            if (row==null) {
                LayoutInflater inflater=((Activity)context).getLayoutInflater();
                inflater.inflate(R.layout.film_list,null);
            }    
            ((TextView)row.findViewById(R.id.film_name)).setText(data[position].filmName);
            ((TextView)row.findViewById(R.id.imdb_rating)).setText(data[position].imdbRating);
            return row;    
        }
    也可以改成这样
    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null) {
                LayoutInflater inflater=((Activity)context).getLayoutInflater();
                convertView = inflater.inflate(R.layout.film_list,null);
            }    
            ((TextView)convertView.findViewById(R.id.film_name)).setText(data[position].filmName);
            ((TextView)convertView.findViewById(R.id.imdb_rating)).setText(data[position].imdbRating);
            return row;    
        }
SachinKS
SachinKS 太厉害了。。谢谢你
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!