2012-12-03 85 views
2

創建自定義的ListView我想有一個自定義的ListView,所以我這樣做:UnsupportedOperationException異常在Android的

創建了一個活動:

public class PRS_MainList_Act extends ListActivity { 
    MainListAdapter mladp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_prs__main_list_); 
     ArrayList<MainMenuItem> menuItems = new ArrayList<MainMenuItem>(); 
     menuItems.add(new MainMenuItem(getResources().getString(R.string.main_list_connectivity), getResources().getString(R.string.main_list_connectivityDesc))); 
     this.mladp = new MainListAdapter(this, R.layout.man_list_item, R.id.textView1, menuItems); 
     setListAdapter(mladp); 

    } 
} 

創建菜單項的類:

public class MainMenuItem { 

    private String itemText; 
    private String itemDescription; 
    private String itemIMG; 
    private int itemWeight; 
    private String itemAssociatedActivity; 

    public MainMenuItem(String itemText, String itemDescription) { 
     this.itemText = itemText; 
     this.itemDescription = itemDescription; 
    } 

    public String getItemText() { 
     return itemText; 
    } 
    public void setItemText(String itemText) { 
     this.itemText = itemText; 
    } 
    public String getItemIMG() { 
     return itemIMG; 
    } 
    public void setItemIMG(String itemIMG) { 
     this.itemIMG = itemIMG; 
    } 
    public int getItemWeight() { 
     return itemWeight; 
    } 
    public void setItemWeight(int itemWeight) { 
     this.itemWeight = itemWeight; 
    } 
    public String getItemAssociatedActivity() { 
     return itemAssociatedActivity; 
    } 
    public void setItemAssociatedActivity(String itemAssociatedActivity) { 
     this.itemAssociatedActivity = itemAssociatedActivity; 
    } 

    public String getItemDescription() { 
     return itemDescription; 
    } 

    public void setItemDescription(String itemDescription) { 
     this.itemDescription = itemDescription; 
    } 

} 

定製陣列適配器:

public class MainListAdapter extends ArrayAdapter<MainMenuItem> { 

    ArrayList<MainMenuItem> menuItems; 

    public MainListAdapter(Context context, int resource, 
      int textViewResourceId, ArrayList<MainMenuItem> menuItems) { 
     super(context, resource, textViewResourceId, menuItems); 
     this.menuItems = menuItems; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
      view = vi.inflate(R.layout.man_list_item, parent); 
     } 
     MainMenuItem it = menuItems.get(position); 
     if (it != null) { 
      TextView titleTV = (TextView) view.findViewById(R.id.textView1); 
      TextView descriptionTV = (TextView) view.findViewById(R.id.textView2); 
      ImageView iconIV = (ImageView)view.findViewById(R.id.imageView1); 
      if (titleTV != null) { 
       titleTV.setText(it.getItemText()); 
      } 
      if(descriptionTV != null){ 
       descriptionTV.setText(it.getItemDescription()); 
      } 
      if(iconIV != null){ 
       if(it.getItemText().equals(getContext().getResources().getString(R.string.main_list_connectivity))) 
        iconIV.setImageResource(R.drawable.network_connections); 
      } 
     } 
     return view; 
    } 

} 

,這裏是我的活動佈局和項目佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".PRS_MainList_Act" > 
    <ListView android:id="@android:id/list" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 

</LinearLayout> 

項目佈局:

<?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/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:src="@drawable/ic_launcher" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:text="TextView" /> 

    </LinearLayout> 

</LinearLayout> 

我得到這個錯誤說:java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)不支持in AdapterView

在此先感謝您的幫助。

+0

你可以請包括完整的堆棧跟蹤? –

+0

亞當,Luksprog解決方案工作。是否有必要包含完整的堆棧跟蹤? – Mahyar

+0

我想不到。 –

回答

5

不能使用這個版本的inflate方法:

view = vi.inflate(R.layout.man_list_item, parent); 

,因爲這將充氣View添加到父,這是不是在允許ListView。使用此版本:

view = vi.inflate(R.layout.man_list_item, parent, false); 

這將膨脹視圖,但不會將其添加到父級。此版本非常重要,因爲它將爲您的充氣視圖提供適當的LayoutParams

+0

知府!有效。萬分感謝。 – Mahyar

相關問題