无法从 ListView 项中检索 Intent 的地址和电话号码

Posted

技术标签:

【中文标题】无法从 ListView 项中检索 Intent 的地址和电话号码【英文标题】:Can`t retrieve address and phone number for Intents from ListView item 【发布时间】:2018-12-15 13:52:15 【问题描述】:

我是一名初级 android 开发人员,我正在为我的 Udacity 课程开发一个应用程序。我在 ListView 项中放置了两个按钮 - 一个用于打开地图,另一个用于拨打电话。为了让它们工作我试图从列表中的当前对象中检索地址和电话号码,但它不起作用,即使地图和电话应用程序被打开,它也只显示我的位置而不是对象位置和随机电话号码。

这是来自我的适配器类的代码:

public class LocationAdapter extends ArrayAdapter<Location>
private String address;
//create custom constructor for LocationAdapter
public LocationAdapter (Activity context, ArrayList<Location> locations)
    super (context, 0, locations);

//Provide a View (ListView) for adapter
//@param position - the position of the item in the adapter
//@param convertView - the old view to reuse, if available
//@param parent - the parent view this view will be attached to

@Override
public View getView (int position, View convertView, ViewGroup parent)
    View listItemView = convertView;
    if (listItemView==null)
        listItemView = 
LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    
//Get Location object from this position on the list
    final Location currentLocation = getItem(position);

    //Set onClickListener on Map Button and use an Intent in onClick method 
to open location on map

    final ImageButton mapButton = (ImageButton) 
listItemView.findViewById(R.id.map_button);
    mapButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            String address 
 =Integer.toString(currentLocation.getLocationAddressString());
            Intent openMap = new Intent(Intent.ACTION_VIEW);
            openMap.setData(Uri.parse("geo:"+ address));
            getContext().startActivity(openMap);

        
    );
     //Set onClickListener on Call Button and use an Intent in onClick method 
to open a dialer
    final ImageButton callButton = (ImageButton) 
listItemView.findViewById(R.id.call_button);
    callButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            String number 
=Integer.toString(currentLocation.getLocationPhoneNumberString());
            Intent makeCall = new Intent (Intent.ACTION_DIAL);
            makeCall.setData(Uri.parse("tel:"+number));
            getContext().startActivity(makeCall);
        
    );


    //Find ImageView and set an Image of the current Location object
    ImageView locationImage =(ImageView) 
listItemView.findViewById(R.id.image);
    locationImage.setImageResource(currentLocation.getImageResourceId());

    //Find Location Name TextView and set Name of the current Location object
    TextView locationName =(TextView) listItemView.findViewById(R.id.name);
    locationName.setText(currentLocation.getLocationNameString());

    //Find Location Description TextView and set Description of the current 
Location object
    TextView locationDescription = (TextView) 
listItemView.findViewById(R.id.description);
    
locationDescription.setText(currentLocation.getLocationDescriptionString());

    return listItemView;

 

这是位置类代码:

public class Location 
//Image associated with Location
private int mImageResourceId;
//Location name
private int mLocationNameResourceId;
//Location description
private int mLocationDescriptionResourceId;
//Location coordinates
private int mLocationAddressResourceId;
//Location phone number
private int mLocationPhoneNumberResourceId;

//Create a constructor for the Location Object
public Location (int ImageResourceId, int LocationNameResourceId, int 
LocationDescriptionResourceId,
                 int LocationAddressResourceId, int 
LocationPhoneNumberResourceId) 
    mImageResourceId=ImageResourceId;
    mLocationNameResourceId=LocationNameResourceId;
    mLocationDescriptionResourceId=LocationDescriptionResourceId;
    mLocationAddressResourceId=LocationAddressResourceId;
    mLocationPhoneNumberResourceId=LocationPhoneNumberResourceId;
  
  //Get image resource id
public int getImageResourceId()
    return mImageResourceId;
  
//get Location name
public int getLocationNameString()
    return mLocationNameResourceId;

//get Location description
public int getLocationDescriptionString()
    return mLocationDescriptionResourceId;

//get Location address
public int getLocationAddressString()
    return mLocationAddressResourceId;

//get Location phone number
public int getLocationPhoneNumberString()
    return mLocationPhoneNumberResourceId;



已解决

因此,问题在于将 LocationAddress 作为 Integer 存储在自定义类中,而不是将其存储为 String。

【问题讨论】:

【参考方案1】:

认为这可能与地址 URI 在这一行的创建方式有关:

openMap.setData(Uri.parse("geo:"+ address));

基本上,这将输出一个看起来像这样的 addressUri:

geo:123 Sesame St

但是查看https://developers.google.com/maps/documentation/urls/android-intents 的示例需要进行一些更改:

1) 使用地址时应将纬度/经度设置为 0,0 2) 您实际上必须指定 Uri 的 ?q= 部分

所以我认为如果您将前面提到的行更改为此,它应该可以工作:

openMap.setData(Uri.parse("geo:0,0?q="+ address));

【讨论】:

大家好,我已尝试按照您的建议进行更改,但仍然无法正常工作。

以上是关于无法从 ListView 项中检索 Intent 的地址和电话号码的主要内容,如果未能解决你的问题,请参考以下文章

无法从 ListView 通过 Intent 正确传递数据

uiautomator - 当我验证每个列表项中的文本时,无法让 ListView 滚动。当我点击屏幕上的最后一个项目时它就失败了

尝试将数据从Intent迁移到ListView,但出现了一些错误

自定义 ListView 以处理从另一个 Intent 传递的 EditText 和 TextView

从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android)

如何从自定义 ListView 适配器传递 Intent Extra?