我的列表视图没有填充

时间:2017-07-05 10:29:55

标签: android xml listview android-cursor

这是我的java代码 每当我运行代码时,没有显示错误,只运行搜索栏和按钮,而不是带有任何项目的列表视图。我在手机上运行代码而不是模拟器。请帮忙

public class songs extends ListActivity {
    private static final int UPDATE_FREQUENCY = 500;
    private static final int STEP_VALUE = 4000;
    private MediaCursorAdapter mediaAdapter = null;
    private TextView selectedFile = null;
    private SeekBar seekbar = null;
    private MediaPlayer player = null;
    private ImageButton playButton = null;
    private ImageButton prevButton = null;
    private ImageButton nextButton = null;
    private ListView list = null;
    private boolean isStarted = true;
    private String currentFile = "";
    private boolean isMovingSeekbar = false;

    private final Handler handler = new Handler();

    private final Runnable updatePositionRunnable = new Runnable() {
        @Override
        public void run() {
            updatePosition();
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.songs);

        selectedFile = (TextView) findViewById(R.id.selectedfile);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        playButton = (ImageButton) findViewById(R.id.play);
        prevButton = (ImageButton) findViewById(R.id.prev);
        nextButton = (ImageButton) findViewById(R.id.next);
        list = (ListView) findViewById(android.R.id.list);
        player = new MediaPlayer();
        player.setOnCompletionListener(onCompletion);
        player.setOnErrorListener(onError);
        seekbar.setOnSeekBarChangeListener(seekBarChanged);

        player.setAudioStreamType(AudioManager.STREAM_MUSIC);

        Uri uri = MediaStore.Files.getContentUri("external");

        String[] projection = null;

        String sortOrder = null;

        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";

        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp3");

        String[] selectionArgsMp3 = new String[]{mimeType};

        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selectionMimeType, selectionArgsMp3, sortOrder);

        if (null != cursor && cursor.moveToFirst()) {
            cursor.moveToFirst();
            mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
            setListAdapter(mediaAdapter);

            playButton.setOnClickListener(onButtonClick);
            nextButton.setOnClickListener(onButtonClick);
            prevButton.setOnClickListener(onButtonClick);
        }
        cursor.close();

    }

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);

        currentFile = (String) view.getTag();
        startPlay(currentFile);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(updatePositionRunnable);
        player.stop();
        player.reset();
        player.release();
        player = null;

    }

    private void startPlay(String file) {
        Log.i("Selected:", file);

        selectedFile.setText(file);
        seekbar.setProgress(0);
        player.stop();
        player.reset();

        try {
            player.setDataSource(file);
            player.prepare();
            player.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        seekbar.setMax(player.getDuration());
        playButton.setImageResource(android.R.drawable.ic_media_pause);
        updatePosition();
        isStarted = true;

    }

    private void stopPlay() {
        player.stop();
        player.reset();
        playButton.setImageResource(android.R.drawable.ic_media_play);
        handler.removeCallbacks(updatePositionRunnable);
        seekbar.setProgress(0);
        isStarted = false;

    }

    private void updatePosition() {
        handler.removeCallbacks(updatePositionRunnable);
        seekbar.setProgress(player.getCurrentPosition());
        handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
    }

    private class MediaCursorAdapter extends SimpleCursorAdapter {
        public MediaCursorAdapter(Context context, int layout, Cursor c) {
            super(context, layout, c, new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
                    new int[]{R.id.displayname, R.id.duration});
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView title = (TextView) view.findViewById(R.id.title);
            TextView name = (TextView) view.findViewById(R.id.displayname);
            TextView duration = (TextView) view.findViewById(R.id.duration);

            name.setText(cursor.getString(
                    cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

            title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));

            long durationInMs = Long.parseLong(cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));

            double durataionInMin = ((double) durationInMs / 1000.0) / 60.0;

            durataionInMin = new BigDecimal(Double.toString(durataionInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();

            duration.setText("" + durataionInMin);

            view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.listitem, parent, false);

            bindView(v, context, cursor);
            return v;

        }
    }

    private View.OnClickListener onButtonClick = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.play: {
                    if (player.isPlaying()) {
                        handler.removeCallbacks(updatePositionRunnable);
                        player.pause();
                        playButton.setImageResource(android.R.drawable.ic_media_play);
                    } else {
                        if (isStarted) {
                            player.start();

                            playButton.setImageResource(android.R.drawable.ic_media_pause);

                            updatePosition();
                        } else {
                            startPlay(currentFile);
                        }

                    }
                    break;
                }
                case R.id.next: {
                    int seekto = player.getCurrentPosition() + STEP_VALUE;

                    if (seekto > player.getDuration())
                        seekto = player.getDuration();

                    player.pause();
                    player.seekTo(seekto);
                    player.start();
                    break;
                }
                case R.id.prev: {
                    int seekto = player.getCurrentPosition() + STEP_VALUE;

                    if (seekto < 0)
                        seekto = 0;

                    player.pause();
                    player.seekTo(seekto);
                    player.start();
                    break;
                }

            }
        }
    };

    private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            stopPlay();
        }
    };

    private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;

        }
    };

    private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {

        public void onStopTrackingTouch(SeekBar seekBar) {
            isMovingSeekbar = false;
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            isMovingSeekbar = true;
        }

        @Override

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (isMovingSeekbar) {
                player.seekTo(progress);

                Log.i("OnSeekBarChangeListener", "onProgressChanged");
            }
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);//Menu Resource, Menu
        return true;
    }

    @Override


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                Toast.makeText(getApplicationContext(), "Playing All Songs", Toast.LENGTH_LONG).show();
                return true;
            case R.id.two:
                Toast.makeText(getApplicationContext(), "Rename The Song", Toast.LENGTH_LONG).show();
                return true;
            case R.id.three:
                Toast.makeText(getApplicationContext(), "Adding Ringtone", Toast.LENGTH_LONG).show();
                return true;
            case R.id.four:
                Toast.makeText(getApplicationContext(), "Choose Playlist", Toast.LENGTH_LONG).show();
                return true;
            case R.id.five:
                Toast.makeText(getApplicationContext(), "Song deleted", Toast.LENGTH_LONG).show();
                return true;
            case R.id.six:
                Toast.makeText(getApplicationContext(), "Songs Deleted", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

}

这是我的song.xml文件

        <?xml version="1.0" encoding="utf-8"?>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical" android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingTop="16dp"
                    android:paddingBottom="16dp"
                   >

                    <ListView
                        android:id="@android:id/list"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.0" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@android:drawable/screen_background_light"
                        android:orientation="vertical"
                        android:padding="10dip">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/selectedfile"
                            android:ellipsize="middle"
                            android:gravity="center_horizontal"
                            android:singleLine="true"
                            android:text="No file selcted"
                            android:textColor="@android:color/black"
                            />

                        <SeekBar
                            android:id="@+id/seekbar"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:max="100"
                            android:padding="10dip"/>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@android:drawable/screen_background_light"
                            android:gravity="center"
                            android:orientation="horizontal">

                            <ImageButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/prev"
                                android:src="@android:drawable/ic_media_previous"/>

                            <ImageButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/play"
                                android:src="@android:drawable/ic_media_play"/>

                            <ImageButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/next"
                                android:src="@android:drawable/ic_media_next"/>

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

这是我的listenitem.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/displayname"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="18dip"
        android:textStyle="bold"
        android:layout_weight="0.75" />

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="303dp"
            android:layout_height="104dp"
            android:id="@+id/title"
            android:ellipsize="end"
            android:singleLine="true"
            android:textSize="15dip"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/duration"
            android:ellipsize="end"
            android:singleLine="true"
            android:textSize="15dip"
            />

    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它对我有用。唯一的问题是,列表显示在屏幕的右上角:

After clicking on the vertical three dots