我制作了listview,从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;
}
}
main.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>
Listview行
<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>
答案 0 :(得分:1)
你不能像这样膨胀你的观点:
row = inflater.inflate(R.layout.listview_item_row, parent);
相反,你应该:
row = inflater.inflate(R.layout.listview_item_row, parent, false);
这会告诉parent
,在这种情况下为ListView
,不会附加孩子。