我有一个列表视图和搜索字段的活动,当我点击某个项目时,它会加载一个新视图,其内容与所点击的内容相关...
无论如何,这一切都很好,但是,现在我添加了搜索字段,它已经不能用了......
我想出了问题,但我无法理解我应该将它替换为......
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString(); //PROBLEM
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), BotafogoSingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
这个家伙String product = ((TextView) view).getText().toString();
就是问题..
当我查看调试面板时,这就是我得到的: java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView
我猜这条线正在获取线性布局而不是它内部的TextView?
答案 0 :(得分:1)
OnItemClick
的{{1}}参数是整个列表项的视图。在这种情况下,您似乎从使用每个list_item的单个TextView转换为使用自定义LinearLayout(我假设包含您的TextView)。
在这种情况下,您需要用
替换产品系列view
其中your_text_view是与TextView相关联的android:id。