有没有办法自定义列表视图中的项目名称?

时间:2015-10-09 05:18:02

标签: java android listview android-sdcard

我正在使用listview制作一个mp3播放列表,它会检索存储在SD卡中的所有mp3文件的路径,并在项目点击时播放它们。但我得到的是文件的路径显示在播放列表中,我希望显示歌曲的名称而不是它。有没有办法在显示播放列表时按项目名称替换项目路径?我需要访问文件路径,因此无法仅将名称添加到列表中。 这是我的代码:

package com.example.mp3;


import java.io.File;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;

import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {

    ListView list;
    ArrayAdapter<String> listAdapter ;
    ArrayList<String> listTest;
    ImageButton play,stop,back,next;
    String songpath,song;
    File[] listFile;
    SharedPreferences sharedPref;
    MediaPlayer mp;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        song = sharedPref.getString("songname", "name");

        mp=new MediaPlayer();

        mp.setOnCompletionListener(this);


        list = (ListView)findViewById(R.id.list);
        listTest = new ArrayList<String>( );

        play = (ImageButton)findViewById(R.id.play);
        back = (ImageButton)findViewById(R.id.prev);
        next = (ImageButton)findViewById(R.id.next);

        //adding listeners
        play.setOnClickListener(this);
        back.setOnClickListener(this);
        next.setOnClickListener(this);


        //action bar controls
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));

        Scanner("/sdcard/");////storage path


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        if(listTest.size() != 0)
        {
            listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listTest);
            list.setAdapter(listAdapter);
            list.setOnItemClickListener(new OnItemClickListener() 
            {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) //can two text be accessed here??
            {


                //accessing the song name
                String name = (String) ((TextView) view).getText();
                //Log.e(TAG, name);

                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
                try{
                    mp.reset();
                    mp.setDataSource(name);//source
                    mp.prepare();
                    mp.start();
                    play.setImageResource(R.drawable.pause);
                    }
                catch(Exception e){e.printStackTrace();}


            }


            });




            }

        }



        ////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////*Songs added here to list*////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        private void Scanner(String path) {
            // TODO Auto-generated method stub
            {
                try 
                {
                        File fl = new File(path);
                        File[] listOfFiles = fl.listFiles();              

                        for (File listOfFile : listOfFiles)
                         {
                            String s = listOfFile.getName();

                            if(s.endsWith(".mp3"))
                            {

                            songpath = listOfFile.getPath();
                            listTest.add(songpath);//adding song names to list


                            }


                            /////////////////////////////////
                            File f = new File(path+s+"/");
                            if (f.exists() && f.isDirectory()) {
                            Scanner(path+s+"/");
                            }
                            ////////////////////////////////


                        }



                }
            catch (Exception e) { }
            }

            }








    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v.equals(play))
        {
            if(mp.isPlaying())
            {
                mp.pause();
                Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
                //change in button image//
                play.setImageResource(R.drawable.play);

            }
            else
            {
                mp.start();
                Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
                //change in button image//
                play.setImageResource(R.drawable.pause);
                //
            }
        }


            if (v.equals(back))
            {
                mp.setDataSource(name);
                    Toast.makeText(MainActivity.this, "back", Toast.LENGTH_SHORT).show();



        }       

    }


    @Override
    public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPause() {
        super.onPause();
        mp.stop();
        Toast.makeText(getApplicationContext(), "paused", Toast.LENGTH_LONG).show();

    }

}

已更新

package com.example.mp3;


import java.io.File;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;

import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {

    ListView list;
    ArrayAdapter<String> listAdapter ;
    ArrayList<String> listTest;
     ArrayList<String> listSoundNames;
    ImageButton play,stop,back,next;
    String songpath,song;
    File[] listFile;
    SharedPreferences sharedPref;
    MediaPlayer mp;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        song = sharedPref.getString("songname", "name");

        mp=new MediaPlayer();

        mp.setOnCompletionListener(this);


        list = (ListView)findViewById(R.id.list);
        listTest = new ArrayList<String>( );
        listSoundNames=new ArrayList<String>();

        play = (ImageButton)findViewById(R.id.play);
        back = (ImageButton)findViewById(R.id.prev);
        next = (ImageButton)findViewById(R.id.next);

        //adding listeners
        play.setOnClickListener(this);
        back.setOnClickListener(this);
        next.setOnClickListener(this);


        //action bar controls
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));

        Scanner("/sdcard/");////storage path


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        if(listTest.size() != 0)
        {
            listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listTest);
            list.setAdapter(listAdapter);
            list.setOnItemClickListener(new OnItemClickListener() 
            {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) //can two text be accessed here??
            {


                //accessing the song name
                String name = (String) ((TextView) view).getText();
                //Log.e(TAG, name);

                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
                try{
                    mp.reset();
                    mp.setDataSource(listTest.get(position));//source
                    mp.prepare();
                    mp.start();
                    play.setImageResource(R.drawable.pause);
                    }
                catch(Exception e){e.printStackTrace();}



            }


            });




            }

        }



        ////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////*Songs added here to list*////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        private void Scanner(String path) {
            // TODO Auto-generated method stub
            {
                try 
                {
                        File fl = new File(path);
                        File[] listOfFiles = fl.listFiles();              

                        for (File listOfFile : listOfFiles)
                         {
                            String s = listOfFile.getName();

                            if(s.endsWith(".mp3"))
                            {

                            songpath = listOfFile.getPath();
                            listTest.add(songpath);//adding song names to list
                            //listTest.toString().replaceFirst(songpath, s);



                            // store file name in listSoundNames
                            int pos = s.lastIndexOf(".");
                            if (pos > 0)
                            {
                                song = s.substring(0, pos);
                            }
                            listSoundNames.add(song);

                            }


                            /////////////////////////////////
                            File f = new File(path+s+"/");
                            if (f.exists() && f.isDirectory()) {
                            Scanner(path+s+"/");
                            }
                            ////////////////////////////////


                        }



                }
            catch (Exception e) { }
            }

            }








    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v.equals(play))
        {
            if(mp.isPlaying())
            {
                mp.pause();
                Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
                //change in button image//
                play.setImageResource(R.drawable.play);

            }
            else
            {
                mp.start();
                Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
                //change in button image//
                play.setImageResource(R.drawable.pause);
                //
            }
        }


            if (v.equals(back))
            {
                //mp.setDataSource(name);
                    Toast.makeText(MainActivity.this, "back", Toast.LENGTH_SHORT).show();



        }       

    }


    @Override
    public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPause() {
        super.onPause();
        mp.stop();
        Toast.makeText(getApplicationContext(), "paused", Toast.LENGTH_LONG).show();

    }

}

1 个答案:

答案 0 :(得分:1)

  

我得到的是文件的路径显示在   播放列表

因为在listTest列表中添加文件的路径而不是声音文件名。

  

有没有办法在项目名称替换项目名称时   显示播放列表?

listTest中添加文件路径时,使用单独的ArrayList同时存储文件名:

ArrayList<String> listSoundNames;
listSoundNames=new ArrayList<String>();
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0) {
    fileName = s.substring(0, pos);
}
listSoundNames.add(fileName);

listSoundNames作为数据源传递给ArrayAdapter,然后单击ListView项目,使用listTest ArrayList获取要播放的文件路径:

mp.setDataSource(listTest.get(position));