我在一个安卓应用程序中使用了一个列表视图。每行列表视图由两个文本视图组成,一个文本视图(命名text1
)用于显示数据库中的一个单词,另一个文本视图(命名text2
)用于显示数据库中该词的含义。列表视图的显示效果很好。每当我单击列表视图的行(项目)时,我想从列表视图中显示单词和含义。但是这个动作效果不好,它正在“强制关闭”。我使用了这段代码:
@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)
我应该怎么做才能从列表视图中显示两个 rextview 的字符串?请任何人告诉我我做错了什么以及解决方案是什么????