エラー:
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)
次のメソッド実装で見つかりました:
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();
}
});
79 行目:
String itemString = (String)parent.getItemAtPosition(position);
Java クラス:
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();
}
});
}
なぜこのエラーが発生するのですか? 誰か助けてください。listView クリックでアイテムの内容を取得しようとしています