我在Android应用中使用了listview。 listview的每一行都包含两个textview,一个textview(名为text1
)用于显示数据库中的单词,另一个textview(名为text2
)用于显示数据库中该单词的含义。显示listview的工作正常。每当我点击列表视图的一行(项目)时,我想从列表视图中显示单词和含义。但是这种行为效果不好,正在“被迫关闭”。
我用了这段代码:
@Override
public void afterTextChanged(Editable s) {
mDbHelper.open();
String ss=s.toString();// get the word from the edittext
ArrayList<HashMap<String, String>> list_of_wordmeanings = new ArrayList<HashMap<String, String>>();
Cursor mCursor = mDbHelper.getTestData(ss);// get the rows from the database matched with EditText data
for (int i = 0; i < mCursor.getCount(); i++)
{
mCursor.moveToPosition(i);
HashMap<String, String> hm = new HashMap<String, String>();
String word = mCursor.getString(0).toString();
String meaning = mCursor.getString(1).toString();
hm.put("key_word",word);
hm.put("key_meaning",meaning);
list_of_wordmeanings.add(hm);
}
String[] from = { "key_word","key_meaning" };
int[] to = { R.id.txt1,R.id.txt2};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_wordmeanings, R.layout.list_layout, from, to);
listView.setAdapter(adapter);
} // end of afterTextChanged
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String english_word =
cursor.getString(0);
String word_meaning =
cursor.getString(1);
Toast.makeText(getApplicationContext(),english_word+word_meaning, Toast.LENGTH_SHORT).show();
}
我的LogCat显示了这些:
08-05 10:34:13.470: W/KeyCharacterMap(15079): No keyboard for id 0
08-05 10:34:13.470: W/KeyCharacterMap(15079): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-05 10:34:15.150: W/dalvikvm(15079): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-05 10:34:15.159: E/AndroidRuntime(15079): FATAL EXCEPTION: main
08-05 10:34:15.159: E/AndroidRuntime(15079): java.lang.ClassCastException: java.util.HashMap
08-05 10:34:15.159: E/AndroidRuntime(15079): at com.example.chondrokotha5.MainActivity$2.onItemClick(MainActivity.java:136)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.widget.ListView.performItemClick(ListView.java:3513)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.os.Handler.handleCallback(Handler.java:587)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.os.Looper.loop(Looper.java:123)
08-05 10:34:15.159: E/AndroidRuntime(15079): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-05 10:34:15.159: E/AndroidRuntime(15079): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 10:34:15.159: E/AndroidRuntime(15079): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 10:34:15.159: E/AndroidRuntime(15079): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 10:34:15.159: E/AndroidRuntime(15079): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 10:34:15.159: E/AndroidRuntime(15079): at dalvik.system.NativeStart.main(Native Method)
08-05 10:35:13.449: W/KeyCharacterMap(15090): No keyboard for id 0
08-05 10:35:13.449: W/KeyCharacterMap(15090): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-05 10:35:16.250: W/dalvikvm(15090): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-05 10:35:16.260: E/AndroidRuntime(15090): FATAL EXCEPTION: main
08-05 10:35:16.260: E/AndroidRuntime(15090): java.lang.ClassCastException: java.util.HashMap
08-05 10:35:16.260: E/AndroidRuntime(15090): at com.example.chondrokotha5.MainActivity$2.onItemClick(MainActivity.java:136)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.widget.ListView.performItemClick(ListView.java:3513)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.os.Handler.handleCallback(Handler.java:587)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.os.Looper.loop(Looper.java:123)
08-05 10:35:16.260: E/AndroidRuntime(15090): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-05 10:35:16.260: E/AndroidRuntime(15090): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 10:35:16.260: E/AndroidRuntime(15090): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 10:35:16.260: E/AndroidRuntime(15090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 10:35:16.260: E/AndroidRuntime(15090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 10:35:16.260: E/AndroidRuntime(15090): at dalvik.system.NativeStart.main(Native Method)
我该怎么做才能从listview中显示两个rextview的字符串? 请有人告诉我我做错了什么,解决方案是什么?
答案 0 :(得分:0)
在你的onclick中尝试这个..它对我有用..
HashMap<String, String> sss = (HashMap<String, String>) listView.getItemAtPosition(position);
Toast.makeText(getBaseContext(), sss.get("key_word")+"---"+sss.get("key_meaning"), Toast.LENGTH_SHORT).show();
为什么你收到一个类强制转换例外,你已经在列表视图中填充了 list_of_wordmeanings hashmap中的数据:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_wordmeanings, R.layout.list_layout, from, to);
但是您正尝试在listview onItemClick中检索游标对象。