必須在我的應用程序中自定義標準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的偉大工程,但我不會:((
我解決它不受與NULL替換親本,但與添加布爾型的第三parametd。但現在當我滾動ListView到底部時,它會拋出ArraiIndexOutOfBoundsException!爲什麼? – mmmaaak 2012-04-27 08:59:16
,因爲你正在使用的數據[位置]其中只有3個項目的位置值將大於2. – 2012-04-27 09:02:55
ou f * uck它的真實))謝謝 – mmmaaak 2012-04-27 09:07:41