Android onclick操作在listview中无法正常执行
我在android应用程序中使用了listview。listview的每一行由两个文本视图组成,一个文本视图(名为Android onclick操作在listview中无法正常执行,android,android-listview,Android,Android Listview,我在android应用程序中使用了listview。listview的每一行由两个文本视图组成,一个文本视图(名为text1)用于显示数据库中的一个单词,另一个文本视图(名为text2)用于显示数据库中该单词的含义。listview的显示效果良好。每当我单击listview的一行(项目)时,我都想显示listview中的单词和含义。但这一行动并不奏效,它正在“被迫关闭”。 我使用了以下代码: @Override public void afterTextChanged(Edita
text1
)用于显示数据库中的一个单词,另一个文本视图(名为text2
)用于显示数据库中该单词的含义。listview的显示效果良好。每当我单击listview的一行(项目)时,我都想显示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();
}
如何从listview中显示两个rextview的字符串??
请任何人告诉我我做错了什么,解决方法是什么???在你的点击中尝试一下。。这对我很管用
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();
但是您正在尝试在listview onItemClick中检索游标对象。您可以发布堆栈跟踪吗?我想您已经在EditText-afterTextChanged方法中写入了listview onClickListener。我将它放在EditText-afterTextChanged方法之后,然后还显示了错误,并在我的问题中发布了它。请检查此项。@AndroidKiller再次检查我的问题问题请@kalyanpvs从我的LogCat消息中我如何理解这些错误@Tamilan@Bayazid只需复制错误(
java.lang.ClassCastException:java.util.HashMap
)并在谷歌上搜索它。。你会了解他们的。
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();
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_wordmeanings, R.layout.list_layout, from, to);