2012-04-27 1 views
0

私のアプリケーションで標準のListViewコントロールをカスタマイズする必要があります。Androidリストビューをカスタマイズする

私はLynda.comのビデオチュートリアルを見て、彼らが示したすべてを行いました。

My活動レイアウト:

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

私のカスタムリストビューの行レイアウト:

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

今では問題私が作成する方法のdoesn私のリストの構造、いくつかの "ハードコーディング"があります))。

は、私は私の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のコレクションです。それはエラーなしでビルドされますが、このアクティビティを開始しようとすると、次のようなメッセージでクラッシュします。" E/AndroidRuntime(495): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView"

すでにこの問題を抱えている人はいますか?素晴らしい作品が、私にはないビデオチュートリアルカスタムリストビューでは:((

答えて

1

コードの下

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

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

を交換私はないNULLと親に置き換えることによってそれを解決するが、ブール型の第parametdを追加しています。しかし今、私はListViewを下にスクロールすると、ArraiIndexOutOfBoundsExceptionがスローされます。なぜ ??? – mmmaaak

+0

ポジション値で3つのアイテムしか持たないデータ[position]を使用しているため、 –

+0

ou f * uck true))thanks – mmmaaak

3

変更:

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 comparissonsため==演算子を使用しますが、を呼び出すことはありません。。