简体   繁体   English

如何获取我的Alertdialog显示列表视图信息?

[英]how can i get my alertdialog to display listview info?

my alertdialog wont display the textview from my listview. 我的alertdialog将不会从列表视图显示textview。 when i click on the listview it only displays the first list of the list view string. 当我单击列表视图时,它仅显示列表视图字符串的第一个列表。 could someone please help me! 有人可以帮我!

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

     }
   });
 }
}

(Better late than never, right?) (迟到总比不到好,对吧?)

Instead of this: 代替这个:

       int selectedPosition = 0;
       adb.setTitle(""+taxi[selectedPosition]);
       adb.setMessage("Phone Number: "+number[selectedPosition]);
Make use of the int position parameter the onItemClick method receives. 利用onItemClick方法接收的int position参数。 So, the three lines of code above might then become: 因此,上面的三行代码可能会变成:
  int selectedPosition = position; adb.setTitle(""+taxi[selectedPosition]); adb.setMessage("Phone Number: "+number[selectedPosition]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM