0

我制作了从 sd 卡加载很多东西并在 Listview 中显示的列表视图。现在我正在尝试制作自定义列表视图适配器以使该列表视图看起来更好,但每次我都会收到此错误02-22 20:54:21.411: E/AndroidRuntime(17273): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 这是我的 java 代码:

 musiclist = (ListView) findViewById(R.id.PhoneMusicList);
           musiclist.setAdapter(new MusicAdapter(getApplicationContext(), count, null));

    public class MusicAdapter extends BaseAdapter {
           private Context mContext;
           Context context; 
        int layoutResourceId;    
        ListView data[] = null;
           public MusicAdapter(Context context, int layoutResourceId, ListView data[]){
               super();
               mContext = context;

                this.layoutResourceId = layoutResourceId;
                this.context = context;
                this.data = data;
           }

           public int getCount() {
                 return count;
           }

           public Object getItem(int position) {
                 return position;
           }

           public long getItemId(int position) {
                 return position;
           }


           public View getView(int position, View convertView, ViewGroup parent) {
               View row = convertView;
               ListViewHolder holder = null ;

                 String id = null;
               if(row == null)
            {
                   LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.listview_item_row, parent);
             holder = new ListViewHolder();
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                       musiccursor.moveToPosition(position);
                       id = musiccursor.getString(music_column_index);
                       music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                       musiccursor.moveToPosition(position);
                       id += musiccursor.getString(music_column_index);
                      row.setTag(holder);
                      holder.txtTitle.setText(id);
                      holder.imgIcon.setImageResource(R.drawable.music);
                      holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
                    holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
                 } else {
                       holder = (ListViewHolder)row.getTag();
                 }
               holder.txtTitle.setText(id);
               holder.imgIcon.setImageResource(R.drawable.music);
                 return row;

           }

        }
     static class ListViewHolder
        {
            ImageView imgIcon;
            TextView txtTitle;
 }
}

主.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent">

        <ListView
            android:id="@+id/PhoneMusicList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        </ListView>

    </LinearLayout>

列表视图行

         <ImageView android:id="@+id/imgIcon"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

         <TextView android:id="@+id/txtTitle"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:textStyle="bold"
            android:textSize="22dp"
            android:textColor="#000000"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp" />

    </LinearLayout>
4

1 回答 1

1

你不能像这样夸大你的视图:

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

相反,您应该:

row = inflater.inflate(R.layout.listview_item_row, parent, false);

这将告诉parent,在这种情况下ListView,不要附加孩子。

于 2013-02-22T20:14:57.330 回答