ビューフリッパーにメディアストアのアーティストを含むリストが表示されるアクティビティがあります。このリストをクリックすると、選択したアーティストのアルバムのリストが表示され、そのアルバムの曲が表示されます。曲がクリックされると、テキストビューに文字列「title」が入力されます。
この時点まで、すべてのカーソルは正常に機能していますが、最後のカーソルはどういうわけか位置がずれているようです。logcatが私に言っている理由を誰かに教えてもらえますか?
05-07 23:58:54.195: E/AndroidRuntime(1961): java.lang.IllegalStateException: Couldn't read row 3, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
読めない特定の行は、選択したアーティスト/アルバム/曲によって異なります。コードは次のとおりです。ご助力ありがとうございます。
package music.flipper;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.provider.MediaStore.Audio.ArtistColumns;
import android.provider.MediaStore.Audio.AudioColumns;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MusicFlipper extends Activity implements OnItemClickListener {
/** Called when the activity is first created. */
ViewFlipper viewflipper;
Cursor cursor;
private String currentList = "Artist";
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
//set the main view to flipper.
viewflipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
String[] columns = {
BaseColumns._ID,
ArtistColumns.ARTIST
};
//The columns to return for each row.
cursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
columns, null, null, null);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
//set an onitemclicklistener to the first listview in flipper
String[] displayFields = new String[] { ArtistColumns.ARTIST };
//set all the artist names to the array 'displayfields'
int[] displayViews = new int[] { R.id.rowItem };
//number of rows to display and where to bind them
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter); }
//Take the display fields array, and bind to the matching display row
@SuppressWarnings("deprecation")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if( currentList.equals("Artist")) {
if (cursor.moveToPosition(position)) {
//once an item is clicked, move the cursor to that items position
String where = AudioColumns.ARTIST + "=?";
// Have the cursor look within the artist row?
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(AlbumColumns.ARTIST)) };
//Choose the particular row with the chosen artist's name
String[] columns = {
BaseColumns._ID,
AudioColumns.ALBUM,
};
String orderBy = BaseColumns._ID;
cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView2);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { AudioColumns.ALBUM };
int[] displayViews = new int[] { R.id.rowItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
currentList = "Album";
viewflipper.showNext();}
} if (currentList.equals("Album")) {
if (cursor.moveToPosition(position)) {
String where = AudioColumns.ALBUM
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(AlbumColumns.ALBUM)) };
String[] columns = {
MediaColumns.DATA,
BaseColumns._ID,
MediaColumns.TITLE,
MediaColumns.DISPLAY_NAME,
MediaColumns.MIME_TYPE,
};
String orderBy = MediaColumns.TITLE;
cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView3);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { MediaColumns.TITLE };
int[] displayViews = new int[] { R.id.rowItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
currentList.equals("Songs");
viewflipper.showNext();}
} if (currentList.equals("Songs")) {
if (cursor.moveToPosition(position)) {
String title = cursor.getString(cursor.getColumnIndex(MediaColumns.TITLE));
TextView myTextView = (TextView) findViewById(R.id.title);
myTextView.setText(title);
}
}
}
}