游标无法按预期工作

时间:2015-09-30 23:22:01

标签: android

我创建了一个音乐播放器,可以生成歌曲对象列表,将它们存储在歌曲对象列表数组中,并将歌曲对象列表数组传递给自定义数组适配器,以便在列表视图中显示。但是,列表视图显示相同的歌曲对象,例如

enter image description here

主要课程:

private ListView listView1;

// A list of song objects
private ArrayList<Song> songObectList = new ArrayList<>();

// Song object will be passed to SongObjectList
private Song songObject = new Song();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Add song objects to songObjectList
    getMusic();

    // Initialize adapter
    SongAdapter adapter = new SongAdapter(this, R.layout.listview_item_row, songObectList);

    // Initialize listView
    listView1 = (ListView)findViewById(R.id.listView1);

    // Initialize header
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);

    // Add header to listView
    listView1.addHeaderView(header);

    // Add adapter to listView
    listView1.setAdapter(adapter);

创建歌曲对象列表数组方法(在主类中定义)

private void getMusic() {

    // Set URI
    Uri contentURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    // Set projection parameter for getContentResolver query
    String[] projection = {
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION
    };

    // Run getContentResolver query
    final Cursor mCursor = getContentResolver().query(
            contentURI, projection, null, null, null
    );

    // Set song object properties 
    if (mCursor.moveToFirst()) {
        do {

            songObject.album = mCursor.getString(0);
            songObject.artist = mCursor.getString(1);
            songObject.title = mCursor.getString(2);
            songObject.data = mCursor.getString(3);
            songObject.duration = mCursor.getString(4);

            songObectList.add(songObject);

        } while (mCursor.moveToNext());
    }

    mCursor.close();

    return;
}

对于那些对效率/设计感兴趣的人,请参阅log cat以获取错误和建议。

安装程序后记录cat

10-01 12:59:13.410  17534-17534/? E/Zygote﹕ MountEmulatedStorage()
10-01 12:59:13.410  17534-17534/? E/Zygote﹕ v2
10-01 12:59:13.410  17534-17534/? I/libpersona﹕ KNOX_SDCARD checking this for 10339
10-01 12:59:13.410  17534-17534/? I/libpersona﹕ KNOX_SDCARD not a persona
10-01 12:59:13.440  17534-17534/? I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SAMSUNG-SM-N900A_5.0 ver=27
10-01 12:59:13.440  17534-17534/? I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SAMSUNG-SM-N900A_5.0_0027
10-01 12:59:13.440  17534-17534/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
10-01 12:59:13.440  17534-17534/? I/art﹕ Late-enabling -Xcheck:jni
10-01 12:59:13.470  17534-17534/? D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
10-01 12:59:13.470  17534-17534/? D/ActivityThread﹕ Added TimaKeyStore provider
10-01 12:59:13.500  17534-17534/com.example.michael.musicplayer D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.example.michael.musicplayer-1/base.apk
10-01 12:59:13.520  17534-17534/com.example.michael.musicplayer V/MediaPlayer-JNI﹕ native_setup
10-01 12:59:13.520  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ constructor
10-01 12:59:13.520  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ setListener
10-01 12:59:13.520  17534-17534/com.example.michael.musicplayer E/MediaPlayer-JNI﹕ QCMediaPlayer mediaplayer NOT present
10-01 12:59:13.570  17534-17534/com.example.michael.musicplayer D/AbsListView﹕ Get MotionRecognitionManager
10-01 12:59:13.600  17534-17534/com.example.michael.musicplayer D/Activity﹕ performCreate Call secproduct feature valuefalse
10-01 12:59:13.600  17534-17534/com.example.michael.musicplayer D/Activity﹕ performCreate Call debug elastic valuetrue
10-01 12:59:13.640  17534-17554/com.example.michael.musicplayer D/OpenGLRenderer﹕ Render dirty regions requested: true
10-01 12:59:13.690  17534-17554/com.example.michael.musicplayer I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.025_msm8974_LA.BF.1.1_RB1__release_AU ()
    OpenGL ES Shader Compiler Version: E031.25.01.03
    Build Date: 11/19/14 Wed
    Local Branch: mybranch5813579
    Remote Branch: quic/LA.BF.1.1_rb1.11
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.025 + 30e7589 +  NOTHING
10-01 12:59:13.690  17534-17554/com.example.michael.musicplayer I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-01 12:59:13.710  17534-17554/com.example.michael.musicplayer I/OpenGLRenderer﹕ HWUI protection enabled for context ,  &this =0xb3922088 ,&mEglDisplay = 1 , &mEglConfig = 8
10-01 12:59:13.710  17534-17554/com.example.michael.musicplayer D/OpenGLRenderer﹕ Enabling debug mode 0
10-01 12:59:13.850  17534-17534/com.example.michael.musicplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2a393d05 time:874084

我在节目上播放歌曲后记录猫

10-01 13:00:51.260  17534-17534/com.example.michael.musicplayer D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer W/TAG -﹕ path: /storage/emulated/0/Playlists/#75 Little Routine - Glasidum (2015).mp3
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer W/TAG -﹕ path: /storage/emulated/0/Playlists/#75 Little Routine - Glasidum (2015).mp3
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer V/MediaPlayer-JNI﹕ reset
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ reset
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer V/MediaPlayer-JNI﹕ setDataSourceFD: fd 37
10-01 13:00:51.420  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ setDataSource(37, 0, 576460752303423487)
10-01 13:00:51.430  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=8, ext1=0, ext2=0
10-01 13:00:51.430  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ notify(8, 0, 0) callback on disconnected mediaplayer
10-01 13:00:51.450  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ setVideoSurfaceTexture
10-01 13:00:51.450  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ prepare
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=200, ext1=973, ext2=0
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer W/MediaPlayer﹕ info/warning (973, 0)
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ callback application
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ back from callback
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=5, ext1=0, ext2=0
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ New video size 0 x 0
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ callback application
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ back from callback
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=1, ext1=0, ext2=0
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ prepared
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ signal application thread
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ callback application
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ prepare complete - status=0
10-01 13:00:51.460  17534-17548/com.example.michael.musicplayer V/MediaPlayer﹕ back from callback
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer E/MediaPlayer﹕ Should have subtitle controller already set
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ getAudioStreamType
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer V/MediaPlayer-JNI﹕ getAudioStreamType: 3 (streamtype)
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer V/MediaPlayer-JNI﹕ start
10-01 13:00:51.460  17534-17534/com.example.michael.musicplayer V/MediaPlayer﹕ start
10-01 13:00:51.460  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=300, ext1=0, ext2=0
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ arg0: android.widget.ListView{2fdc7175 VFED.VC. .F....ID 0,0-1080,1677 #7f0c004d app:id/listView1}
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ arg1: android.widget.LinearLayout{ec250a V.E..... ........ 0,150-1080,368}
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ arg2: 1
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ arg3: 0
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ songObectList.get(arg2).data: /storage/emulated/0/Music/music/Erroll Garner Concert By The Sea(jazz)(mp3@320)[rogercc][h33t]/01 I'll Remember April.mp3
10-01 13:00:51.490  17534-17534/com.example.michael.musicplayer W/TAG -﹕ songObectList.get(arg2).title: 01 I'll Remember April
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ Received SEC_MM_PLAYER_CONTEXT_AWARE
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ callback application
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ back from callback
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ message received msg=6, ext1=0, ext2=0
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ Received MEDIA_STARTED
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ callback application
10-01 13:00:51.490  17534-17545/com.example.michael.musicplayer V/MediaPlayer﹕ back from callback
10-01 13:00:51.500  17534-17534/com.example.michael.musicplayer I/MediaPlayer﹕ Don't send intent. msg.arg1 = 0, msg.arg2 = 0
10-01 13:00:51.500  17534-17534/com.example.michael.musicplayer E/MediaPlayer﹕ Should have subtitle controller already set

1 个答案:

答案 0 :(得分:1)

您的问题是每次都将相同的对象添加到列表中,因为您已将其定义为类的属性。每次要添加新的SongObject时,都必须创建每个SongObject的新实例,因为当您更改其属性时,实际上是将属性更改为列表的每个元素(它们都指向相同的SongObject)。

快速解决方法是删除属性

// Song object will be passed to SongObjectList
private Song songObject = new Song();

在每次迭代的循环中声明该对象:

// Set song object properties 
if (mCursor.moveToFirst()) {
    do {
        Song songObject = new Song();

        songObject.album = mCursor.getString(0);
        songObject.artist = mCursor.getString(1);
        songObject.title = mCursor.getString(2);
        songObject.data = mCursor.getString(3);
        songObject.duration = mCursor.getString(4);

        songObectList.add(songObject);

    } while (mCursor.moveToNext());
 }