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 :((