我已经制作了一个艺术家列表,一旦点击就会显示一个专辑列表。这个想法是,一旦点击一个专辑,就会出现歌曲列表。
不幸的是,因为在此活动中生成了两个listviews,所以第二个'onItemClick'方法在eclipse中被标记为'duplicate'。有没有人知道如何解决这个问题?
以下是主要活动代码:
package music.library;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MusicLibrary extends Activity implements OnItemClickListener {
Cursor cursor;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] columns = {
MediaStore.Audio.Artists._ID,
MediaStore.Audio.Artists.ARTIST };
cursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
columns, null, null, null);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { MediaStore.Audio.Artists.ARTIST };
int[] displayViews = new int[] { R.id.artistItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.artist_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
}
@SuppressWarnings("deprecation")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
};
String where = android.provider.MediaStore.Audio.Media.ARTIST
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ARTIST)) };
String orderBy = android.provider.MediaStore.Audio.Media._ID;
cursor = managedQuery(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns,
where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields = new String[] { MediaStore.Audio.Media.ALBUM };
int[] displayViews = new int[] { R.id.albumItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.album_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
}
}
@SuppressWarnings("deprecation")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String where = MediaStore.Audio.Media.ALBUM
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };
String orderBy = MediaStore.Audio.Media._ID;
cursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields =
new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
int[] displayViews = new int[] { R.id.songItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.song_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
}
}
}
感谢您的帮助。
---- ----编辑
谢谢你的回答。虽然所有答案都很棒,但我选择了dymmeh的答案作为我的“接受”答案,因为将观点分成不同的活动似乎最有意义(即使这不是我所说的我在寻找的内容)最初的问题)。
我最终创建了以下内容:
头等舱,展示艺术家:
package music.library;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MusicLibrary extends Activity implements OnItemClickListener {
Cursor cursor;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] columns = {
MediaStore.Audio.Artists._ID,
MediaStore.Audio.Artists.ARTIST
};
cursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
columns, null, null, null);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { MediaStore.Audio.Artists.ARTIST };
int[] displayViews = new int[] { R.id.artistItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.artist_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter); }
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
String where = android.provider.MediaStore.Audio.Media.ARTIST
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ARTIST)) };
Intent intent = new Intent(MusicLibrary.this, AlbumSelection.class);
intent.putExtra("where", where);
intent.putExtra("whereVal", whereVal);
startActivity(intent);
}
}
}
第二节课,展示专辑:
package music.library;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AlbumSelection extends Activity implements OnItemClickListener {
Cursor cursor;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
String where = extras.getString("where");
String[] whereVal = extras.getStringArray("whereVal");
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
};
String orderBy = android.provider.MediaStore.Audio.Media._ID;
cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { MediaStore.Audio.Media.ALBUM };
int[] displayViews = new int[] { R.id.albumItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.album_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter); }
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
String where = android.provider.MediaStore.Audio.Media.ALBUM
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };
Intent intent = new Intent(AlbumSelection.this, SongSelection.class);
intent.putExtra("where", where);
intent.putExtra("whereVal", whereVal);
startActivity(intent);
}
}
}
第三节课,播放歌曲:
package music.library;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class SongSelection extends Activity implements OnItemClickListener {
Cursor cursor;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
String where = extras.getString("where");
String[] whereVal = extras.getStringArray("whereVal");
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 orderBy = android.provider.MediaStore.Audio.Media.TITLE;
cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { MediaStore.Audio.Media.TITLE };
int[] displayViews = new int[] { R.id.songItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.song_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter); }
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
/*Intent intent = new Intent(AlbumSelection.this, SongSelection.class);
intent.putExtra("ARTIST", artist);
startActivity(intent);
*/
}
}
}
这一切都很好用。
感谢所有回复。我希望听到关于如何整理/提高代码效率的任何反馈。
再次感谢。
答案 0 :(得分:5)
我找到了这个解决方案。 View.getId()
将返回值-1。所以我们无法知道该项目是否被点击。
请改用adapter.getId()
。它将返回正确的项目ID。
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
if (adapter.getId() == R.id.ls_menu_genre) {
//item was clicked
}
}
答案 1 :(得分:4)
您应该使用以下方法:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
只有一次。然后你可以做类似的事情:
v.getId()
并做这样的事情:
@SuppressWarnings("deprecation")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if( v.getId() == R.id.listView ){
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String where = MediaStore.Audio.Media.ALBUM
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };
String orderBy = MediaStore.Audio.Media._ID;
cursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields =
new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
int[] displayViews = new int[] { R.id.songItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.song_item, cursor, displayFields, displayViews);
}
listView.setAdapter(adapter);
}else{
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
};
String where = android.provider.MediaStore.Audio.Media.ARTIST
+ "=?";
String whereVal[] = { cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ARTIST)) };
String orderBy = android.provider.MediaStore.Audio.Media._ID;
cursor = managedQuery(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns,
where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields = new String[] { MediaStore.Audio.Media.ALBUM };
int[] displayViews = new int[] { R.id.albumItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.album_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
}
}
}
答案 2 :(得分:2)
我认为有两种方法可以做到这一点
1。)将第二个onItemClick方法内容移动到第一个。 有一个标志,用于确定您是否在艺术家或专辑列表视图中,并基于此在onItemClick方法中执行功能。
if (artistMode)
{
//do artist mode stuff for clicks
}
else
{
//do album mode stuff for clicks
}
2.。)采用更“标准”的方式,将专辑和艺术家观点分成不同的活动。这样,您就不会在一个文件中管理两组代码,而这些代码通常可以在两个活动中完成。
答案 3 :(得分:0)
只使用一个onItemClick,然后检查你得到的AdapterView,并在那里设置if
来处理这两个列表。但是你需要有不同的适配器名称,调用它们“适配器”在尝试这样做时不会起作用。
编辑
错过了您重新使用相同的列表视图。新的大头钉。设置变量并使用它来区分。
在MusicLibrary中添加:
private String currentList = "Artist"; // set the tracking variable to your initial list
现在你的onClick应该是这样的:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if (currentList.equals("Artist")) {
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
};
String where = android.provider.MediaStore.Audio.Media.ARTIST + "=?";
String whereVal[] = { cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)) };
String orderBy = android.provider.MediaStore.Audio.Media._ID;
cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields = new String[] { MediaStore.Audio.Media.ALBUM };
int[] displayViews = new int[] { R.id.albumItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.album_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
currentList = "Album"; // re-set the list variable
} else if (currentList.equals("Album")) {
if (cursor.moveToPosition(position)) {
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String where = MediaStore.Audio.Media.ALBUM + "=?";
String whereVal[] = { cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };
String orderBy = MediaStore.Audio.Media._ID;
cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, where, whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listView);
String[] displayFields =
new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
int[] displayViews = new int[] { R.id.songItem };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.song_item, cursor, displayFields, displayViews);
listView.setAdapter(adapter);
currentList = "Songs"; // re-set the list variable
}
}
}