2012-04-27 148 views
0

必须在我的应用程序中自定义标准ListView控件。自定义Android ListView

我查看了Lynda.com的视频教​​程,并做了他们展示的所有内容。

我的动态布局:

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

我定制的ListView列布局:

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

所以我创造我自己的LiasAdapter类:

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

现在事情没有按我怎么创建我的列表的结构,所以有一些“硬编码”))。

我设置该适配器为我的ListView控件:

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

因此, “数据” 是有效的ArrayList集合。它建立withiut错误,但是当我tryed启动这项活动与它这样的消息崩溃:" E/AndroidRuntime(495): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView"

如果有人已经遇到了这个问题?在视频教程定制的ListView的伟大工程,但我不会:((

回答

1

替换下面的代码

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

View row = inflater.inflate(R.layout.custom_menu_row, null); 
+0

我解决它不受与NULL替换亲本,但与添加布尔型的第三parametd。但现在当我滚动ListView到底部时,它会抛出ArraiIndexOutOfBoundsException!为什么? – mmmaaak 2012-04-27 08:59:16

+0

,因为你正在使用的数据[位置]其中只有3个项目的位置值将大于2. – 2012-04-27 09:02:55

+0

ou f * uck它的真实))谢谢 – mmmaaak 2012-04-27 09:07:41

3

变化:

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

到:

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

你有可能会很容易地发现,做一个快速的搜索这里在计算器上。例如this hit

另外,不要使用==运营商在Java String comparissons,但拨打string.equals("otherString")