我是一名初学者 Android 开发人员,正在为我的 Udacity 类(class)开发应用程序。我在 ListView 项中放置了两个按钮 - 一个用于打开 map ,另一个用于调用电话。为了使它们工作,我尝试从列表中的当前对象检索地址和电话号码,但它不起作用,即使 map 和电话应用程序打开,它也只显示我的位置,而不是对象位置和随机电话数量。
这是我的 Adapdter 类中的代码:
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。
最佳答案
认为这可能与此行上地址 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));
关于java - 无法从 ListView 项目中检索 Intent 的地址和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51219123/