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

Is there any way to customize the names of the items in a 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();

    }

}

what I am getting is the paths of the files are displayed in the playlist

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

Is there any way to replace the item path by item name when the playlist is displayed?

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));