我正在尝试从我的listView中显示一个项目,我没有在eclipse上收到任何错误但是如果我单击一个项目我的应用程序表明它正在关闭。
这是我使用toast`
的代码 private void registerClickCallback() {
ListView list1 = (ListView) findViewById(R.id.listView1);
list1.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int postion, long id) {
TextView textView = (TextView) viewClicked;
String message = "This is the message: " + textView.getText().toString();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});`
这是我的列表
的数组适配器 `private class MyListAdapter extends ArrayAdapter<Messages>{
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, messages);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// make sure we have a view to work with
View itemView = convertView;
if (itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
// find the message to work with
Messages currentMessages = messages.get(position);
// fill the view for icon (The icon used in my view)
ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentMessages.getIconID());
// the message, setting the message written to the message on my view
TextView textMessage = (TextView) itemView.findViewById(R.id.item_message);
textMessage.setText(currentMessages.getMessage());
// the sender, filling the sender to the sender on my view
TextView textSender = (TextView) itemView.findViewById(R.id.item_sender);
textSender.setText(currentMessages.getSender());
return itemView; // getting the item view back
}
} // custom adapter ends`
我不确切地知道问题所在,任何帮助都会受到赞赏
答案 0 :(得分:2)
此行就在这里
TextView textView = (TextView) viewClicked;
不是你如何获得textview
你需要
TextView textView = (TextView) viewClicked.findViewById(r.id.textviewid);
答案 1 :(得分:-1)
// list item click replace with this one
private void registerClickCallback() {
ListView list1 = (ListView) findViewById(R.id.listView1);
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int postion, long id) {
//TextView textView = (TextView) viewClicked;
// String message = "This is the message: " + textView.getText().toString();
String message = list1 .getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});`