我正在尝试在cusstom数组上发送OnClickListener。每个元素都有2个文本视图,但我不在乎哪个是按。我只是去按下列表行(歌曲或歌手)时选择了Musicclass值的另一项活动。
这是主要活动“列表”:
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Music> songs = new ArrayList<Music>();
//words.add("one");
songs.add(new Music("Under Pressure","Queen"));
songs.add(new Music("My Way","Frank Sinatra"));
songs.add(new Music("Stressed Out","21 Pilots"));
songs.add(new Music("Despacito","Luis Fonzi"));
songs.add(new Music("Shape of You","Ed Sheeran"));
songs.add(new Music("Believer","Imagine Dragons"));
MusicAdapter adapter = new MusicAdapter(this, songs);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
// Set a click listener to go to the player activity
listView.setOnClickListener(new AdapterView.OnClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Create a new intent to open the {@link NumbersActivity}
Intent playerIntent = new Intent(MainActivity.this, Player.class);
// Start the new activity
startActivity(playerIntent);
}
});
}
}
// //设置单击侦听器后..... //无效,我使用AdapterView会收到错误消息,因此如果需要,可以忽略前面的注释“ //”之后的代码。播放器的意图只是将来以文本味精开始一个简单的活动,我想用歌曲和歌手姓名填充文本视图。
这是自定义适配器:
public class MusicAdapter extends ArrayAdapter<Music>{
public MusicAdapter(Context context, ArrayList<Music> music) {
super(context, 0, music);
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Music currentMusic = getItem(position);
// Find the TextView in the list_item.xml layout with the ID miwok_text_view.
TextView songTextView = (TextView) listItemView.findViewById(R.id.song_text_view);
// Get the Miwok translation from the currentWord object and set this text on
// the Miwok TextView.
songTextView.setText(currentMusic.getSongTranslation());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
artistTextView.setText(currentMusic.getArtistTranslation());
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
}
这是音乐课:
public class Music {
private String mSong;
private String mArtist;
public Music(String song, String artist) {
mSong = song;
mArtist = artist;
}
/**
* Get the default translation of the word.
*/
public String getSongTranslation() {
return mSong;
}
/**
* Get the Miwok translation of the word.
*/
public String getArtistTranslation() {
return mArtist;
}
}
谢谢您的帮助!
答案 0 :(得分:1)
您可以在listItemView
语句之后在视图if
上设置侦听器
listItemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent playerIntent = new Intent(getContext(), Player.class);
// Start the new activity
getContext().startActivity(playerIntent);
}
});