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 =新的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>
最佳答案
在您的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;
}
当行为null时,在getView中,您返回null而不是新创建的行。我认为这是您的问题。
希望这可以帮助...
关于android - 自定义适配器的adapterview错误不支持addview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17538317/