问题描述
- listview 的监听事件中无法显示toast
-
package com.example;import java.util.ArrayList;
import java.util.HashMap;import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class SimpleListViewWithTitle extends ListActivity {
private static final String TAG="outputInformation";
private ListView listview;
private String[] myTitle={"姓名:","性别:","年龄:","居住地:","邮箱:"};
private String[] myStr = {"MARES","男","25","北京","xuanyusong@gmail.com"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList> arrayList= new ArrayList>();
for(int i=0;i
HashMap hashMap= new HashMap();
hashMap.put("myTitle", myTitle[i]);
hashMap.put("myStr", myStr[i]);
arrayList.add(hashMap);
}
listview = getListView();
SimpleAdapter adapter = new SimpleAdapter(this,arrayList,android.R.layout.simple_expandable_list_item_2,new String[]{"myTitle","myStr"},new int[]{android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) {//System.out.println("您选择了" + myTitle[position]+":"+myStr[position]);
Toast.makeText(SimpleListViewWithTitle.this,"您选择了" + myTitle[position]+":"+myStr[position],Toast.LENGTH_LONG).show();Log.i(SimpleListViewWithTitle.TAG,"您选择了" + myTitle[position]+":"+myStr[position]);
}});
setContentView(listview);
}}
解决方案
是不是你的ListView用的系统默认的id,但是写错了。布局文件贴上来看看..