android :listview监听事件无法显示toast

sunny冰青 发布于 2014/09/25 21:08
阅读 848
收藏 0
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<HashMap<String,Object>> arrayList= new ArrayList<HashMap<String,Object>>();
for(int i=0;i<myTitle.length;i++){
 HashMap<String,Object> hashMap= new HashMap<String,Object>();
 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);
}


}

加载中
0
小红书
小红书

去掉最后一行setContentView(listview); 

ListActivity里就个listview

ps:本屌在北京求android开发职位,求骚扰

0
carlos
carlos

我在我的代码中是可以显示Toast的。

你的代码有些问题,setContentView(listview); 这一句怎么放到点击事件里面去了?

有点疑惑。

你放到外面试试看。

s
sunny冰青
谢谢, 最近比较忙没有来得及回复
返回顶部
顶部