如何让我的alertdialog显示listview信息?

时间:2011-01-13 14:06:39

标签: android android-listview

我的alertdialog不会显示我的listview中的textview。当我点击列表视图时,它只显示列表视图字符串的第一个列表。有人可以帮助我!

package com.example.taxime;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FindTaxi extends ListActivity {
 @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   final String[] number = getResources().getStringArray(R.array.number);
   setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,number));

   final String[] taxi = getResources().getStringArray(R.array.taxi);
   setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taxi));

   ListView lv = getListView();
   lv.setTextFilterEnabled(true);

   lv.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {

      AlertDialog.Builder adb=new AlertDialog.Builder(FindTaxi.this);

       int selectedPosition = 0;
       adb.setTitle(""+taxi[selectedPosition]);
       adb.setMessage("Phone Number: "+number[selectedPosition]);
       adb.setPositiveButton(TELEPHONY_SERVICE, null);       
       adb.setNegativeButton("Cancel", null);
       adb.show();

     }
   });
 }
}

1 个答案:

答案 0 :(得分:0)

(迟到总比没有好,对吗?)

而不是:

       int selectedPosition = 0;
       adb.setTitle(""+taxi[selectedPosition]);
       adb.setMessage("Phone Number: "+number[selectedPosition]);
使用int position方法收到的onItemClick参数。因此,上面的三行代码可能会变为:
       int selectedPosition = position;
       adb.setTitle(""+taxi[selectedPosition]);
       adb.setMessage("Phone Number: "+number[selectedPosition]);