Im using listview on android, below code is giving error after clicking on a list item for another new activity
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting list view size
int size = parent.getCount();
// initializing array according to list view size
String[] all_pid = new String[size];
String[] all_name = new String[size];
String[] all_address = new String[size];
String[] all_latitude = new String[size];
String[] all_longitude = new String[size];
// looping data into array according to getItemAtPosition
for (int i = 0; i < size; i++) {
HashMap<String, String> extractor = (HashMap<String, String>) parent.getItemAtPosition(i);
// fill data into array
all_pid[i] = extractor.get("pid");
all_name[i] = extractor.get("name");
all_address[i] = extractor.get("address");
all_latitude[i] = extractor.get("latitude");
all_longitude[i]=extractor.get("longitude");
}
Intent in = new Intent(getApplicationContext(),SingleContactActivityMainFragment.class);
in.putExtra("APid", all_pid);
in.putExtra("AName", all_name);
in.putExtra("AAddress", all_address);
in.putExtra("ALatitude", all_latitude);
in.putExtra("ALongitude", all_longitude);
// added size for initial view pager initialization
in.putExtra("size", size);
in.putExtra("QUOTES", position);
Log.v(DEBUG, "position Passed: " + position);
startActivity(in);
}
Error shown is:
05-27 15:30:12.985: E/AndroidRuntime(23905): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.HashMap
05-27 15:30:12.985: E/AndroidRuntime(23905): at com.example.fightvaw.jsontest.MainActivity$4.onItemClick(MainActivity.java:226)
how to solve these type of error ?
This line
causes your crash.
parent
is an AdapterView and returns an Integer if you callgetItemAtPosition(i)
.You can fix this by replacing
parent
with your ListAdapter (which you needed to fill your ListView). Your code should look like this afterwards:Looks like your parent
AdapterView
is designed for holdingint
values, they will be boxed automatically toInteger
. That is why by callingparent.getItemAtPosition(i)
you getInteger
. Obviously,Integer
cannot be casted toHashMap
this is previous code on my custom adapter:
and this is solved code in my custom adapter:
in which
data
is defined asArrayList<HashMap<String, String>> data;