Custom listview error: java.lang.unsupportedoperationexception: addview

I'm really new to android and I know you shouldn't be pasting whole codes, but I honestly have no idea where the problem is.

I'm trying to create a somewhat custom list view with text and image, but keep getting this exception and don't know how to resolve it.

02-13 13: 23: 06.477: E / AndroidRuntime (1834): java.lang.UnsupportedOperationException: addView (View, LayoutParams) not supported in AdapterView

so hoped that someone here can help me. this is what my launch activity looks like

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView list=(ListView) this.findViewById(R.id.list_view);



        int[] stringIds=new int[2];
        stringIds[0]=R.string.text1;
        stringIds[1]=R.string.text2;

        int[] imgIds=new int[2];
        imgIds[0]=R.drawable.ic_launcher;
        imgIds[1]=R.drawable.ic_launcher;

        CustomAdapter custom=new CustomAdapter(this, stringIds, imgIds);
        list.setAdapter(custom);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

      

My custom adapter:

public class CustomAdapter extends BaseAdapter {

    Context context;
    int[] stringIds;
    int[] imgIds;
    public CustomAdapter(Context context, int[]stringIds, int[] imgIds) {

        this.stringIds=stringIds;
        this.imgIds=imgIds;
        this.context=context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View finalView=convertView;
        if(finalView==null){
             LayoutInflater inflater=((Activity)context).getLayoutInflater();
             finalView= inflater.inflate(R.layout.list_view_row, parent);
        } 

        TextView txt=(TextView)finalView.findViewById(R.id.list_view_row_text_view);
        ImageView img=(ImageView)finalView.findViewById(R.id.imageView1);


        img.setImageResource(imgIds[position]);

        return finalView;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.stringIds.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;

    }
}

      

activity_main.xml layout

<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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

      

list_view_row.xml layout

<?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="vertical" >

   <TextView 
       android:id="@+id/list_view_row_text_view"
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />


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

</LinearLayout>

      

+3


source to share


1 answer


Here:

finalView= inflater.inflate(R.layout.list_view_row, parent);

      

remove parent

finalView= inflater.inflate(R.layout.list_view_row, null);

      

or



finalView= inflater.inflate(R.layout.list_view_row, parent, false);

      

also use

setContentView(R.layout.activity_main);

      

before you get

ListView

+3


source







All Articles