如何从列表视图中获取数据并将其传递给另一个活动

时间:2013-07-18 10:35:12

标签: android listview android-listview listactivity

我的应用会从媒体商店返回相册并将其放入列表视图中。单击相册时,会打开另一个列表视图,其中包含与该相册相关的歌曲。但当我点击任何一首歌时,该应用程序崩溃了。我希望能够获取所选歌曲的文件路径并将其传递给播放该歌曲的另一个活动。这是我的代码:     公共类专辑扩展ListActivity {     光标光标;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
            android.provider.MediaStore.Audio.Albums.ALBUM };

    cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            columns, null, null, null);

    String[] displayFields = new String[] { MediaStore.Audio.Albums.ALBUM };
    int[] displayViews = new int[] { android.R.id.text1 };
    setListAdapter(new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, cursor, displayFields,
            displayViews));

}

protected void onListItemClick(ListView l, View v, int position, long id) {
    if (cursor.moveToPosition(position)) {

        String[] columns = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE, };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";

        String whereVal[] = { cursor.getString(cursor
                .getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };

        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                columns, where, whereVal, orderBy);

        String[] displayFields = new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
        int[] displayViews = new int[] { android.R.id.text1 };

        setListAdapter(new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, cursor, displayFields,
                displayViews));

    }
}


}

这是Logcat中包含的内容:

07-18 17:44:20.327: D/libEGL(30458): loaded /system/lib/egl/libGLES_android.so
07-18 17:44:20.387: D/libEGL(30458): loaded /system/lib/egl/libEGL_adreno200.so
07-18 17:44:20.407: D/libEGL(30458): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
07-18 17:44:20.437: D/libEGL(30458): loaded /system/lib/egl/libGLESv2_adreno200.so
07-18 17:44:20.607: I/Adreno200-EGLSUB(30458): <ConfigWindowMatch:2078>: Format RGBA_8888.
07-18 17:44:20.667: D/memalloc(30458): ashmem: Mapped buffer base:0x51fcf000 size:1536000 fd:62
07-18 17:44:20.697: D/OpenGLRenderer(30458): Enabling debug mode 0
07-18 17:44:20.818: D/memalloc(30458): ashmem: Mapped buffer base:0x52156000 size:1536000 fd:65
07-18 17:44:40.737: D/OpenGLRenderer(30458): has fontRender patch
07-18 17:44:41.047: D/memalloc(30458): ashmem: Mapped buffer base:0x526bd000 size:1536000 fd:68
07-18 17:44:43.750: I/Adreno200-EGLSUB(30458): <ConfigWindowMatch:2078>: Format RGBA_8888.
07-18 17:44:43.770: D/memalloc(30458): ashmem: Mapped buffer base:0x52834000 size:1536000 fd:71
07-18 17:44:43.960: D/memalloc(30458): ashmem: Mapped buffer base:0x52bab000 size:1536000 fd:78
07-18 17:44:43.980: D/OpenGLRenderer(30458): Flushing caches (mode 0)
07-18 17:44:44.981: D/memalloc(30458): ashmem: Mapped buffer base:0x51ecf000 size:1536000 fd:63
07-18 17:44:47.454: E/CursorWindow(30458): Failed to read row 4, column -1 from a CursorWindow which has 11 rows, 5 columns.
07-18 17:44:47.454: D/AndroidRuntime(30458): Shutting down VM
07-18 17:44:47.454: W/dalvikvm(30458): threadid=1: thread exiting with uncaught exception (group=0x40ac7228)
07-18 17:44:47.494: E/AndroidRuntime(30458): FATAL EXCEPTION: main
07-18 17:44:47.494: E/AndroidRuntime(30458): java.lang.IllegalStateException: Couldn't read row 4, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWindow.nativeGetString(Native Method)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWindow.getString(CursorWindow.java:491)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWrapper.getString(CursorWrapper.java:118)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.project.helixplayer.Albums.onListItemClick(Albums.java:49)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.app.ListActivity$2.onItemClick(ListActivity.java:326)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView.performItemClick(AbsListView.java:1077)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2533)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView$1.run(AbsListView.java:3198)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Handler.handleCallback(Handler.java:605)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Looper.loop(Looper.java:154)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.app.ActivityThread.main(ActivityThread.java:4945)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at dalvik.system.NativeStart.main(Native Method)
07-18 17:44:49.846: D/Process(30458): killProcess, pid=30458
07-18 17:44:49.846: D/Process(30458): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-18 17:44:49.846: D/Process(30458): java.lang.Thread.getStackTrace(Thread.java:599)
07-18 17:44:49.846: D/Process(30458): android.os.Process.killProcess(Process.java:788)
07-18 17:44:49.846: D/Process(30458): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
07-18 17:44:49.846: D/Process(30458): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
07-18 17:44:49.846: D/Process(30458): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
07-18 17:44:49.846: D/Process(30458): dalvik.system.NativeStart.main(Native Method)
07-18 17:44:49.846: I/Process(30458): Sending signal. PID: 30458 SIG: 9

1 个答案:

答案 0 :(得分:1)

编辑您的问题并添加LogCat。我不知道你在itemClick上做了什么(如果我不理解的话,请纠正我):你使用cursor.moveToPosition(position)而不确定你把光标放在哪里;然后你采取列和其他查询人员;最后你创建一个列表。

长话短说,您在点击某个项目时似乎正在创建一个列表! 以这种方式更改方法代码:

@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
    // Get the cursor, positioned to the corresponding row in the result set
    Cursor cursor = (Cursor) listViev.getItemAtPosition(position);
    // Find your data on cursor and launch your second activity in order to show album's tracks
}

修改

在我们讨论下面的评论后,我认为你必须改变你的项目结构。

添加包含您的相册列表的拳头活动;当您点击一个项目时,启动第二个活动,其中包含一个专辑曲目列表。

如果使用数据库,查询和显示数据会更容易。