带有自定义布局的UnsupportedOperationException

时间:2011-08-28 02:48:11

标签: java android android-listview

我正在尝试创建一个自定义列表视图来保存自定义对象的视图,在这种情况下,自定义对象将是类Data的实例

这是Java代码:

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListsActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Data[] array = new Data[3];
        Data p1 = new Data("Person 1", 20);
        Data p2 = new Data("Person 2", 33);
        Data p3 = new Data("Person 3", 22);
        array[0] = p1;
        array[1] = p2;
        array[2] = p3;
        setListAdapter(new MyAdapter(this, R.layout.row, array));
    }

    private class MyAdapter extends ArrayAdapter<Data>{

        private Data[] data;
        public MyAdapter(Context context, int textViewResourceId,
                Data[] objects) {
            super(context, textViewResourceId, objects);
            data = objects;
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            convertView = inflater.inflate(R.layout.row, parent);
        }
        TextView text = (TextView) findViewById(R.id.textView1);
        if(data != null){
            text.setText(data[position].getName());
        }
        return convertView;
    }

}  

}

class Data{
    private String name;
    private int age;

public Data(String name, int age) {
    this.name = name;
    this.setAge(age);
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
    }

}

和xml文件 row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal" android:weightSum="1">

    <ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
        android:layout_height="wrap_content" android:layout_width="wrap_content" />

    <TextView android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:text="TextView" android:paddingTop="25dp"
        android:layout_width="match_parent" />

</LinearLayout>

和main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:layout_height="wrap_content" android:id="@android:id/list"
        android:layout_width="match_parent" />

</LinearLayout>

但是它抛出了UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)

2 个答案:

答案 0 :(得分:4)

您没有提供行号和异常详情,但我猜这段代码:

if(convertView == null){
    convertView = inflater.inflate(R.layout.row, parent);
}

应该成为

if(convertView == null){
    // you don't have to append to the parent while inflating in Adapter
    convertView = inflater.inflate(R.layout.row, null);
}

试试,否则请包含完整的例外情况和指向代码的行号

答案 1 :(得分:1)

我解决了它

我通过扩展BaseAdapter创建了自己的适配器,然后调用了对象的findViewByID方法convertView

我的适配器的代码如下,以防其他人遇到此问题

private class MyAdapter extends BaseAdapter{

        private List<Data> data;
        private LayoutInflater inflater;
        private TextView text;

        public MyAdapter(Data[] contents){
            data = new ArrayList<Data>();
            for(Data d : contents){
                data.add(d);
            }
            inflater = getLayoutInflater();

        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = inflater.inflate(R.layout.row, null);
            }
            if(data != null){
                text = (TextView) convertView.findViewById(R.id.textView1);
                text.setText(data.get(position).getName());
            }
            return convertView;
        }


        @Override
        public int getCount() {
            return data.size();
        }


        @Override
        public Object getItem(int arg0) {
            return data.get(arg0);
        }


        @Override
        public long getItemId(int position) {
            return data.get(position).hashCode();
        }

    }
}