0

A have to customize standard ListView control in my application.

I viewed video tutorials by Lynda.com for this and do everything they showed.

My Activity Layout:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#ffffff">

<ListView
    android:id="@+id/menuList"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:layout_x="0dp"
    android:layout_y="210dp"
    android:footerDividersEnabled="false">
</ListView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="250dp"
    android:layout_height="110dp"
    android:layout_x="115dp"
    android:layout_y="100dp"
    android:src="@drawable/deserts_log" />

<ImageView
    android:id="@+id/deserts_home_btn"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_x="20dp"
    android:layout_y="20dp"
    android:src="@drawable/green_home" />

</AbsoluteLayout>

My custom listView row 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="horizontal" >

<ImageView
    android:id="@+id/custom_menu_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/custom_menu_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

So I create my own LiasAdapter class:

public class MyAdapter extends ArrayAdapter<String>
{
    public MyAdapter(Context context, int resource, int textViewResourceId,
            List<String> objects) {
        super(context, resource, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.custom_menu_row, parent);
        String[] data = {"Десерт 1", "Десерт 2", "Десерт 3"};
        ImageView iv = (ImageView)row.findViewById(R.id.custom_menu_item);

        if(data[position]=="Десерт 1")
        {
            iv.setImageResource(R.drawable.desert1);
        }

        if(data[position]=="Десерт 2")
        {
            iv.setImageResource(R.drawable.desert2);
        }

        if(data[position]=="Десерт 3")
        {
            iv.setImageResource(R.drawable.desert3);
        }
        return row;
    }
}

Now it doesn matter how I create a structure of my List, so there are some "hardcoding" )).

I set this adapter for my ListView control:

listView = (ListView)findViewById(R.id.menuList);
    MyAdapter adapter = new      MyAdapter(this,R.layout.custom_menu_row,R.id.custom_menu_text,data);
    listView.setAdapter(adapter);

So, "data" is valid ArrayList collection. It builds withiut errors, but when I tryed to start this activity it crashes with such message: " E/AndroidRuntime(495): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView"

If anybody has already met this problem? In video tutorial custom listView works great, but my doesn't :((

Aerrow
  • 12,086
  • 10
  • 56
  • 90
mmmaaak
  • 803
  • 1
  • 18
  • 38

2 Answers2

3

Change:

View row = inflater.inflate(R.layout.custom_menu_row, parent);

to:

View row = inflater.inflate(R.layout.custom_menu_row, parent, false);

You could've easily found that by doing a quick search here on StackOverflow; e.g this hit.

Also, don't use the == operator for String comparissons in Java, but call string.equals("otherString").

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
1

Replace below code

View row = inflater.inflate(R.layout.custom_menu_row, parent);

with

 View row = inflater.inflate(R.layout.custom_menu_row, null);
Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
  • I solved it not by replacing parent with NULL, but with adding third parametd of bool type. But now when I scroll ListView to the bottom it throws ArraiIndexOutOfBoundsException !!!! Why ??? – mmmaaak Apr 27 '12 at 08:59
  • because you are using data[position] which have only 3 items by position value will be greater than 2. – Vivek Kumar Srivastava Apr 27 '12 at 09:02