当用户添加在ListView
中显示的相册时。假设你选择用户写的lovesong专辑
歌曲名称和标题并保存到歌曲表(songid,albumid,songname,songtitle)。
从歌曲表中获取歌曲我使用
public ArrayList<Alldata> getMyData(long id)
{
ArrayList<Alldata> arrayList=new ArrayList<Alldata>();
Cursor c=db.query(DATABASE_TABLE_SONG, new String[]{KEY_SONG_ID,KEY_ALBUM_ID,KEY_SONG_NAME,KEY_SONG_TITEL}, KEY_ALBUM_ID+"="+id, null,null,null,null);
Alldata myDatas;
while(c.moveToNext())
{
myDatas=new Alldata();
myDatas.song_id=c.getLong(c.getColumnIndex(KEY_SONG_ID));
myDatas.album_id=c.getInt(c.getColumnIndex(KEY_ALBUM_ID));
myDatas.song_name=c.getString(c.getColumnIndex(KEY_SONG_NAME)).trim();
myDatas.song_title=c.getString(c.getColumnIndex(KEY_SONG_TITEL)).trim();
arrayList.add(myDatas);
}
c.close();
return arrayList;
}
public void onClick(View v)
{
Alldata data=new Alldata();
AlbumDAO albumDAO=new AlbumDAO(SongActivity.this);
albumDAO.open();
ArrayList<Alldata> mydata=albumDAO.getMyData(albumid);
data=mydata.get(albumid);
Log.v("data.getAlbum_id()",data.getAlbum_id()+"");
albumDAO.close();
}
});
/////////////////// LOGCAT //////////////////////
02-06 18:07:37.948: ERROR/AndroidRuntime(27144): Uncaught handler: thread main exiting due to uncaught exception
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): java.lang.IndexOutOfBoundsException: Invalid location 1, size is 0
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at java.util.ArrayList.get(ArrayList.java:341)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at krishna.ActionClass.SongActivity$2.onClick(SongActivity.java:79)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.View.performClick(View.java:2364)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.View.onTouchEvent(View.java:4179)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.widget.TextView.onTouchEvent(TextView.java:6541)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.View.dispatchTouchEvent(View.java:3709)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.os.Looper.loop(Looper.java:123)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at java.lang.reflect.Method.invoke(Method.java:521)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-06 18:07:38.069: ERROR/AndroidRuntime(27144): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的数据库似乎返回0行,尝试将if循环包装在if(c.moveToFirst())中
if(c.moveToFirst())
{
while(c.moveToNext())
{
myDatas=new Alldata();
myDatas.song_id=c.getLong(c.getColumnIndex(KEY_SONG_ID));
myDatas.album_id=c.getInt(c.getColumnIndex(KEY_ALBUM_ID));
myDatas.song_name=c.getString(c.getColumnIndex(KEY_SONG_NAME)).trim();
myDatas.song_title=c.getString(c.getColumnIndex(KEY_SONG_TITEL)).trim();
arrayList.add(myDatas);
}
}