必须在我的应用程序中自定义标准 ListView 控件。
我为此观看了 Lynda.com 的视频教程并执行了他们展示的所有内容。
我的 Activity 布局:
<?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);
因此,“data”是有效的 ArrayList 集合。它构建时没有错误,但是当我尝试启动此 Activity 时,它崩溃并显示以下消息: "E/AndroidRuntime(495): java.lang.UnsupportedOperationException: AdapterView 不支持 addView(View, LayoutParams)"
有人遇到过这个问题吗?在视频教程中,自定义 listView 效果很好,但我的不行:((
最佳答案
更改:
View row = inflater.inflate(R.layout.custom_menu_row, parent);
至:
View row = inflater.inflate(R.layout.custom_menu_row, parent, false);
通过在 StackOverflow 上快速搜索,您可以轻松找到这一点;例如this hit .
此外,不要在 Java 中使用 ==
运算符进行 String
比较,而是调用 string.equals("otherString")
.
关于java - 自定义Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10347347/