0

我正在使用带有lazyadapter的listview,现在我需要更改项目行高,但这似乎有点棘手。这是我的适配器:

public class AlbumDetailsAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<Songs> data;
    private static LayoutInflater inflater = null;
    private MediaPlayer mediaPlayer;

    public AlbumDetailsAdapter(Activity a, ArrayList<Songs> products) {
        activity = a;
        mediaPlayer = new MediaPlayer();
        data = products;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

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

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.songs_items, parent);

        TextView songNumber = (TextView) vi
                .findViewById(R.id.songs_item_number);
        TextView albumTitle = (TextView) vi
                .findViewById(R.id.songs_item_album_title);
        TextView singer = (TextView) vi
                .findViewById(R.id.songs_item_album_singer);
        final ImageView playSong = (ImageView) vi.findViewById(R.id.songs_item_play);
        ImageView downloadSong = (ImageView) vi
                .findViewById(R.id.songs_item_download);

        if (!data.get(position).getSONG_MP3()
                .equals("http://nelu.burduja.com/musica/songs_with/")) {
            downloadSong.setBackgroundResource(R.drawable.down_yes);
            if (!mediaPlayer.isPlaying()) {
                playSong.setBackgroundResource(R.drawable.play_nr);
            }
            playSong.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.release();
                        playSong.setBackgroundResource(R.drawable.play_nr);
                        mediaPlayer=new MediaPlayer();
                    } else {
                        playSong.setBackgroundResource(android.R.drawable.ic_media_pause);
                        try {
                            mediaPlayer.setDataSource(data.get(position)
                                    .getSONG_MP3());
                            final ProgressDialog bufferingDialog=new ProgressDialog(activity);
                            bufferingDialog.setMessage("Buffering ...");
                            bufferingDialog.show();
                            mediaPlayer.prepareAsync();
                            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                                @Override
                                public void onPrepared(MediaPlayer arg0) {
                                    bufferingDialog.dismiss();
                                    mediaPlayer.start();
                                }
                            });
                            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer arg0) {
                                    playSong.setBackgroundResource(R.drawable.play_nr);
                                    mediaPlayer.release();
                                }
                            });


                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (SecurityException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        ImageView youtube = (ImageView) vi.findViewById(R.id.songs_item_video);
        if (data.get(position).getSONG_MP4().length() > 0) {
            youtube.setBackgroundResource(R.drawable.video_nr);
            youtube.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse(data.get(position).getSONG_MP4()));
                    v.getContext().startActivity(intent);
                }
            });
        }

        albumTitle.setText(data.get(position).getSONG_TITLE());
        singer.setText(data.get(position).getSONG_ARTIST());
        songNumber.setText((position + 1) + "");
        return vi;
    }
}

还有我的 song_item xml 文件:

<?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="50dp"
    android:gravity="center_vertical"
    android:background="@drawable/list_item_sel"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:id="@+id/songs_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="10"
        android:textColor="@android:color/white" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="55"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/songs_item_album_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/songs_item_album_singer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="@android:color/white" />
    </LinearLayout>

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="35"
        android:gravity="right"
        android:orientation="horizontal" >

    <ImageView
        android:id="@+id/songs_item_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/app_name"
        android:background="@drawable/play_pr" />

    <ImageView
        android:id="@+id/songs_item_video"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/app_name"
        android:background="@drawable/video_pr"/>

    <ImageView
        android:id="@+id/songs_item_download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/app_name"
        android:background="@drawable/down_no"/>
    </LinearLayout>
</LinearLayout>

无论我在 xml 中进行什么更改,我都无法更改列表视图的项目高度。谁能告诉我我该怎么做。我还看到我必须尝试放入vi = inflater.inflate(R.layout.songs_items, parent);适配器 getview 方法,但应用程序崩溃并给我一个错误

04-05 11:02:37.502: E/AndroidRuntime(12776): FATAL EXCEPTION: main
04-05 11:02:37.502: E/AndroidRuntime(12776): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.AdapterView.addView(AdapterView.java:471)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at com.outsoucefarm.musicagratis.functions.AlbumDetailsAdapter.getView(AlbumDetailsAdapter.java:55)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.AbsListView.obtainView(AbsListView.java:2033)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.ListView.makeAndAddView(ListView.java:1781)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.ListView.fillDown(ListView.java:672)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.ListView.fillFromTop(ListView.java:732)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.ListView.layoutChildren(ListView.java:1634)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.AbsListView.onLayout(AbsListView.java:1863)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.View.layout(View.java:11278)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.View.layout(View.java:11278)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.View.layout(View.java:11278)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.View.layout(View.java:11278)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.View.layout(View.java:11278)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.os.Looper.loop(Looper.java:137)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at android.app.ActivityThread.main(ActivityThread.java:4440)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at java.lang.reflect.Method.invoke(Method.java:511)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-05 11:02:37.502: E/AndroidRuntime(12776):    at dalvik.system.NativeStart.main(Native Method)

所以请帮我解决这个问题

4

1 回答 1

0

解决了这个问题。该行vi = inflater.inflate(R.layout.songs_items, null);应该是vi = inflater.inflate(R.layout.songs_items, parent, false);

于 2013-04-05T09:47:30.257 回答