2 u012751110 u012751110 于 2014.09.25 17:35 提问

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);
}

}

1个回答

qq787068730
qq787068730   2014.09.26 10:15

是不是你的ListView用的系统默认的id,但是写错了。布局文件贴上来看看..

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!