ItemModel.java
package com.example.poptunemusicplayer;
import java.io.Serializable;
public class ItemModel implements Serializable {
private String name;
public ItemModel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Getting Error due to String songName = (String) listView.getItemAtPosition(i); at listView.setOnItemClickListener;
Code in MainActivity.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String songName = (String) listView.getItemAtPosition(i);
startActivity(new Intent(getApplicationContext(), PlayerActivity.class)
.putExtra("songs", mySongs)
.putExtra("songname", songName)
.putExtra("position", i));
}
});
Trying to set onClick in listView in my application but this errors occurs
Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.poptunemusicplayer, PID: 7781
java.lang.ClassCastException: com.example.poptunemusicplayer.ItemModel cannot be cast to java.lang.String
at com.example.poptunemusicplayer.MainActivity$2.onItemClick(MainActivity.java:112)
at android.widget.AdapterView.performItemClick(AdapterView.java:350)
at android.widget.AbsListView.performItemClick(AbsListView.java:1674)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4085)
at android.widget.AbsListView$10.run(AbsListView.java:6573)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
I have also implemented serachView but getting wrong results after I write something on it and also getting some different results at onClick. For eg;
Suppose in my list I have some items like:
- To You
- Young
- Astronomia
- Love me like you do
- Honest
- Closer
If I type on search "you" its giving me the result:
- To You
- Love me like you do
Its not showing me the "Young" and if I click on "Love me like you do" then its shows me the data of "Young".
Neither fetching the correct data nor correct position.
getFilter() method in CustomAdapter
@Override
public Filter getFilter(){
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint == null | constraint.length() == 0){
filterResults.count = itemsModelsl.size();
filterResults.values = itemsModelsl;
}
else {
List<ItemModel> resultModel = new ArrayList<>();
String searchStr = constraint.toString().toLowerCase();
for (ItemModel itemModel: itemsModelsl){
if (itemModel.getName().contains(searchStr)){
resultModel.add(itemModel);
}
filterResults.count = resultModel.size();
filterResults.values = resultModel;
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itemsModelListFiltered = (List<ItemModel>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
SearchView Code in OptionsMenu() method
case R.id.searchid:
SearchView searchView = (SearchView) item.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Type here to search");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
customAdapter.getFilter().filter(newText);
return true;
}
});
return true;
Which step am I missing here ?
CodePudding user response:
From the looks of it, your ListView
adapter contains instances of the class ItemModel
and not just simple String
s.
java.lang.ClassCastException: com.example.poptunemusicplayer.ItemModel cannot be cast to java.lang.String
To remedy this, change this
String songName = (String) listView.getItemAtPosition(i);
to
ItemModel itemModel = (ItemModel) listView.getItemAtPosition(i);
String songName = itemModel.getName();
and it should no longer crash.
A recommendation on my end would be to look into using RecyclerView
down the road, but that's a topic for another day.