在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>
這裡寫法有問題,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;
}