英文:
ListView displays correctly, but only fetching last item when onclick. Search function on ListView not working as well
问题
ListView正确显示所有内容(标题、艺术家、封面、绘图),但当点击时,只会获取最后一项(Pills),搜索功能也无法过滤歌曲,不起作用,希望能够按歌曲标题进行搜索,还有一个用于getset方法的类也正常工作。
我是Android Studio的初学者,请指导我,非常感谢!
搜索活动:
public class SearchActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> stringArrayList = new ArrayList<>();
MyListAdapter 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",
"Joji",
"DJ Khaled",
"Usher",
"Ed Sheeran",
"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 MyListAdapter(this, mTitle, mArtist, mCoverArt, 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();
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, R.layout.search_item, mTitle);
this.context = context;
this.mTitle = mTitle;
this.mArtist = mArtist;
this.mCoverArt = mCoverArt;
this.mSongLength = mSongLength;
}
public View getView(int position, View view, 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;
}
}
英文:
ListView is displaying all content correctly (title, artist, cover, drawables), however when clicked on, only the last item (Pills) will be fetched,
and the search function is not filtering the songs as well, not working, wish to search by the title of the song, have a class for the getset methods etc as well which is working fine
Am beginner at Android Studio, please guide me, greatly appreciated!
Search Activity:
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",
"Joji",
"DJ Khaled",
"Usher",
"Ed Sheeran",
"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, mTitle, mArtist, mCoverArt, 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, R.layout.search_item, 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, View view, 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;
};
}
答案1
得分: 1
只保留一个,删除另一个。
英文:
> You wrote setOnItemClickListener
twice, the second one will override
> the first one, so the logical is not your wanted.
Only keep one and delete another one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论