问题描述
ListView正确显示了所有内容(标题,艺术家,封面,可绘制内容),但是,当单击时,只会提取最后一项(药片), 并且搜索功能不能同时过滤歌曲,无法正常运行,希望按歌曲名称进行搜索,为getset方法设置类等也可以正常工作
是Android Studio的初学者,请指导我,非常感谢!
搜索活动:
public class SearchActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> stringArrayList = new ArrayList<>();
Mylistadapter adapter;
//ArrayAdapter<String> adapter;
private SongCollection songCollection = new SongCollection();
String[] mTitle = {
"Life is Good","Good as Hell","Perfect","Attention","Say You love Me","Sign of the Times","Lemon","Autopilot","First Time","To U","Fractures","Will He","I'm so Grateful","Stay At Home","The A Team","BIGGER","Find U Again","Pills"
};
String[] mArtist = {
"Drake","Ariana Grande","Ed Sheeran","Joji","Chris brown","Harry Styles","Rihanna","Quinn XCII","Ellie Goulding","Diplo","ILLENIUM","DJ Khaled","Usher","Beyonce","Camila Cabello","Joji"
};
String[] mSongLength = {
"3.96","2.66","4.39","2.15","2.88","5.68","2.45","3.03","3.23","3.95","5.03","3.37","4.98","3.48","4.31","3.77","2.94","3.12"
};
Integer[] mCoverArt = {
R.drawable.life_is_good,R.drawable.good_as_hell,R.drawable.perfect,R.drawable.attention,R.drawable.say_you_love_me,R.drawable.sign_of_the_times,R.drawable.lemon,R.drawable.auto_pilot,R.drawable.first_time,R.drawable.to_u,R.drawable.fractures,R.drawable.will_he,R.drawable.im_so_grateful,R.drawable.stay_at_home,R.drawable.the_a_team,R.drawable.bigger,R.drawable.find_u_again,R.drawable.pills
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// adapter = new ArrayAdapter<>(SearchActivity.this,R.layout.search_item,mTitle,mArtist,mCoverArt,mSongLength);
adapter = new Mylistadapter(this,mSongLength);
listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setonItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
Toast.makeText(getApplicationContext(),adapter.getItem(position),Toast.LENGTH_SHORT).show();
// Todo Auto-generated method stub
String title = listView.getItemAtPosition(position).toString();
Song selectedSong = songCollection.searchByTitle(title);
AppUtil.popMessage(view.getContext(),"Streaming song: " + selectedSong.getTitle());
sendDatatoActivity(selectedSong);
}
});
** Mylistadapter:**
public class Mylistadapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] mTitle;
private final String[] mArtist;
private final Integer[] mCoverArt;
private final String[] mSongLength;
public Mylistadapter(Activity context,String[] mTitle,String[] mArtist,Integer[] mCoverArt,String[] mSongLength) {
super(context,mTitle);
// Todo Auto-generated constructor stub
this.context = context;
this.mTitle = mTitle;
this.mArtist = mArtist;
this.mCoverArt = mCoverArt;
this.mSongLength = mSongLength;
}
public View getView(int position,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.search_item,null,true);
TextView titleTxt = (TextView) rowView.findViewById(R.id.titleTxt);
ImageView coverArt = (ImageView) rowView.findViewById(R.id.image);
TextView artistTxt = (TextView) rowView.findViewById(R.id.titleArtist);
TextView songLength = (TextView) rowView.findViewById(R.id.txtSongLength);
titleTxt.setText(mTitle[position]);
coverArt.setimageResource(mCoverArt[position]);
artistTxt.setText(mArtist[position]);
songLength.setText(mSongLength[position]);
return rowView;
};
}
解决方法
您两次写过
setOnItemClickListener
,第二个将被覆盖 第一个,所以逻辑上不是您想要的。
仅保留一个并删除另一个。