1

在 setAdapter() 命令之后,我收到“adapterview 不支持 addview”。这是我的源代码:

FilmAdapter.java

package com.pabeda.bcps.adapters;
import java.util.List;
import com.pabeda.bcps.R;
import com.pabeda.bcps.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;

    }
}

我正在这样设置适配器:

FilmAdapter filmadapter=new FilmAdapter(this, R.layout.film_list, film);

GridView gw=(GridView) findViewById(R.id.gridView1);
gw.setAdapter(filmadapter);

res > 布局 > 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>
4

1 回答 1

1

在您的 getView 方法中,您没有将 row 设置为任何内容。试试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row=convertView;

    if (row==null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        row = 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;
}

在您的 getView 中,当该行为 null 时,您返回 null 而不是新创建的行。我认为这是你的问题。

希望这可以帮助...

于 2013-07-09T01:16:56.940 回答