Reputation: 13
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;
};
}
Upvotes: 0
Views: 76
Reputation: 13559
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.
Upvotes: 1