SachinKS
2013-07-09 02:40
采纳率: 85.7%
浏览 6.3k

开发时遇到了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条回答 默认 最新

  • 码密码密哄 2013-07-09 03: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;    
            }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题