简体   繁体   English

在Android中创建自定义ListView时出现UnsupportedOperationException

[英]UnsupportedOperationException in creating custom ListView in Android

i wanted to have a custom listView so I did this: 我想要一个自定义listView,所以我这样做了:

Created an Activity: 创建了一个活动:

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

    }
}

Create a class for menu items: 为菜单项创建一个类:

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

}

Custom array adapter: 自定义阵列适配器:

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

}

and here are my activity layout and item layout: 这是我的活动布局和项目布局:

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

Item Layout: 项目布局:

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

I got this error saying: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 我收到此错误消息:java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)

Thanks in advance for your help. 在此先感谢您的帮助。

You can't use this version of the inflate method: 您不能使用此版本的inflate方法:

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

because this will add the inflated View to the parent, which isn't allowed in a ListView . 因为这会将膨胀后的View添加到父级,而ListView不允许这样做。 Instead use this version: 而是使用以下版本:

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

which will inflate the view but will not add it to the parent. 这将使视图膨胀,但不会将其添加到父视图。 This version is important because it will provide the proper LayoutParams for your inflated view. 这个版本很重要,因为它将为您的展开视图提供适当的LayoutParams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM