开发者

UnsupportedOperationException with custom layout

I'm trying to make a custom listview that will hold views of custom objects, in this case, the custom objects will be instances of class Data

Here's the Java code:

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, textViewRe开发者_如何学JAVAsourceId, 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;
    }

}

and the xml files 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>

and 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>

But it throws an UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView


You didn't give the line number and the exception details, but my guess this code :

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

Should become

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

Try that, otherwise please include the full exception and the line number pointing to your code


I solved it

I created my own Adapter by extending BaseAdapter, and then calling the findViewByID method of the object convertView

The code for my adapter follows, in case anyone else runs into this problem

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();
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜