[英]ListView onItemClickListener
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: 79行:
String itemString = (String)parent.getItemAtPosition(position);
The java class: 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();
}
});
}
Why am I getting this error ? 为什么会出现此错误? Can someone please help.
有人可以帮忙吗? I am trying to get the item contents on listView click
我正在尝试在listView上获取项目内容,请单击
您正在将HashMap
对象转换为String
对象,这会导致此异常
here: 这里:
String itemString = (String)parent.getItemAtPosition(position);
what is returned is an HashMap and you're casting it to a String. 返回的是一个HashMap,您正在将其强制转换为String。
try this 尝试这个
String i= String.valueOf(position);
Toast.makeText(getApplicationContext(), i, Toast.LENGTH_LONG).show();
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. 当ArrayList listFill传递到ListView的适配器时,您可以获得一个HashMap对象,该对象包含与单击的项目位置相对应的歌曲的标题,图标和说明。 Thus,
从而,
HashMap<String, Object> itemSong = HashMap<String, Object>parent.getItemAtPosition(position);
String itemString = itemSong.get("SongTitle");
will meet your requirements. 会满足您的要求。
try this 尝试这个
String itemString = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), ""+itemPostition+" "+itemString, Toast.LENGTH_SHORT).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.