Reputation: 5063
An error:
09-26 13:09:38.551: E/AndroidRuntime(25966): FATAL EXCEPTION: main
09-26 13:09:38.551: E/AndroidRuntime(25966): java.lang.ClassCastException: java.util.HashMap
09-26 13:09:38.551: E/AndroidRuntime(25966): at com.example.customlayoutlistview.MainActivity$1.onItemClick(MainActivity.java:79)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.widget.ListView.performItemClick(ListView.java:3584)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1846)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.os.Handler.handleCallback(Handler.java:587)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.os.Looper.loop(Looper.java:130)
09-26 13:09:38.551: E/AndroidRuntime(25966): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-26 13:09:38.551: E/AndroidRuntime(25966): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 13:09:38.551: E/AndroidRuntime(25966): at java.lang.reflect.Method.invoke(Method.java:507)
09-26 13:09:38.551: E/AndroidRuntime(25966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-26 13:09:38.551: E/AndroidRuntime(25966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-26 13:09:38.551: E/AndroidRuntime(25966): at dalvik.system.NativeStart.main(Native Method)
is being found on the following method implementation:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
int itemPostition = position;
String itemString = (String)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), ""+itemPostition+" "+itemString, Toast.LENGTH_SHORT).show();
}
});
Line 79:
String itemString = (String)parent.getItemAtPosition(position);
The java class:
public class MainActivity extends Activity {
ArrayList<HashMap<String, Object>> listFill;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listFill = new ArrayList<HashMap<String, Object>>();
listView = (ListView) findViewById(R.id.listofviews);
// Initializing and defining the contents to be filled
int[] songIcons = { R.drawable.music, R.drawable.music,
R.drawable.music, R.drawable.music, R.drawable.music };
String[] songTitle = { "Song One", "Song Two", "Song Three",
"Song Four", "Song Five" };
String[] songDescription = { "Description One", "Description Two",
"Description Three", "Description Four", "Description Five" };
//Looping through the contents and adding it to our ArrayList
for (int i = 0; i < songIcons.length; i++) {
HashMap<String, Object> toFill = new HashMap<String, Object>();
//Filling the HashMap first
toFill.put("songIcon", songIcons[i]);
toFill.put("songTitle", songTitle[i]);
toFill.put("songDescription", songDescription[i]);
//Filling the HashMap inside the ArrayList
listFill.add(toFill);
}
//Creating adapter for the listView
ListAdapter adapter = new SimpleAdapter(MainActivity.this, listFill,
R.layout.custom_layout, new String[] { "songIcon", "songTitle",
"songDescription" }, new int[] { R.id.list_icon,
R.id.list_title, R.id.list_description });
listView.setAdapter(adapter);
// setListAdapter(adapter);
//Implementing on itemClicked
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
int itemPostition = position;
String itemString = (String)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), ""+itemPostition+" "+itemString, Toast.LENGTH_SHORT).show();
}
});
}
Why am I getting this error ? Can someone please help. I am trying to get the item contents on listView click
Upvotes: 0
Views: 3188
Reputation: 635
try this
String itemString = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), ""+itemPostition+" "+itemString, Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 101
As ArrayList listFill is passed to adapter of your ListView, you can get an HashMap Object containing title, icon and description for the song corresponding to the position of item which is clicked. Thus,
HashMap<String, Object> itemSong = HashMap<String, Object>parent.getItemAtPosition(position);
String itemString = itemSong.get("SongTitle");
will meet your requirements.
Upvotes: 1
Reputation: 168
try this
String i= String.valueOf(position);
Toast.makeText(getApplicationContext(), i, Toast.LENGTH_LONG).show();
Upvotes: 1
Reputation: 4339
here:
String itemString = (String)parent.getItemAtPosition(position);
what is returned is an HashMap and you're casting it to a String.
Upvotes: 1
Reputation: 2386
You are casting a HashMap
object into String
object which cause of this exception
Upvotes: 5