我有一个ArrayList,其中包含一些酒店的详细信息,我想要做的是单击该地址以打开地图意图,然后单击电话号码以打开电话。
任何帮助将不胜感激。 这些是代码段。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
return rootView;
}
这是它的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hotel_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:src="@drawable/hotel" />
<View
android:id="@+id/grad"
android:layout_height="200dp"
android:layout_width="match_parent"
android:background="@drawable/gradient_splash"
android:alpha="0.4" />
<LinearLayout
android:layout_height="200dp"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/category_header"
android:layout_width="match_parent"
android:layout_height="110dp"
android:text="Hotels"
android:gravity="center_vertical"
android:textColor="@color/textBlack"
android:textSize="25sp"
android:textAlignment="center" />
<LinearLayout
android:id="@+id/location_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:paddingLeft="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/location_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_map_" />
<TextView
android:id="@+id/location_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_location"/>
</LinearLayout>
<LinearLayout
android:id="@+id/phone_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp">
<ImageView
android:id="@+id/phone_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_phone_solid" />
<TextView
android:id="@+id/phone_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_phone"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
适配器:
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null){
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
return listView;
}
}
到目前为止,我只有一个意图可以打开拨号程序。如何分别选择“位置”部分和“电话”部分,并为两者分别设置意图?
答案 0 :(得分:0)
尝试一下
首先,我建议使用Recylerview,因为Recylerview是高级版本。
在这里检查:https://developer.android.com/reference/android/support/v7/widget/RecyclerView
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
LinearLayout linear_phone = view.findViewById(R.id.phone_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse(pojo.getPhoneID()));
startActivity(callIntent);
}
});
LinearLayout linear_location = view.findViewById(R.id.location_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Location Intent Code
}
});
}
});
答案 1 :(得分:0)
尝试一下
public class HotelsAdapter extends ArrayAdapter<pojo> {
private Context mContext;
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
this.mContext = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null) {
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
final pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
// add this code in your adapter
LinearLayout locationLayout = (LinearLayout) listView.findViewById(R.id.location_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent geoIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri
.parse("geo:0,0?q=" + currentItem.getAddressID()));
mContext.startActivity(geoIntent);
}
});
LinearLayout phoneLayout = (LinearLayout) listView.findViewById(R.id.phone_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: " + currentItem.getPhoneID()));
mContext.startActivity(callIntent);
}
});
return listView;
}
}
并删除listview.onitemClicklistener();
答案 2 :(得分:0)
如果您不希望整个列表项都是可点击的,而是希望列表中包含可点击的元素,则应实现自定义侦听器
创建一个侦听器界面,为每个可单击的子视图指定一个方法,在“活动/片段”中实现该方法,然后将其传递给自定义列表适配器。
然后,您的自定义适配器将根据单击哪个字段来调用侦听器的方法。
示例界面
public interface HotelListClickListener {
void onAddressClicked(Pojo item);
void onPhoneClicked(Pojo item);
...
}
然后,您应该修改HotelsAdapter以便在某个时候接收侦听器(构造方法或Setter方法),然后在 getView 方法中调用每个适当的方法。
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
...
Pojo currentItem = getItem(position);
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
addressView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onAddressClicked(currentItem);
}
});
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
phoneView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onPhoneClicked(currentItem);
}
});
...
}
最后,在活动/片段中,您将创建一个全局侦听器,当单击特定的子视图时,该侦听器将接收适当的POJO,然后处理单击的任何元素。
HotelListClickListener listener = new HotelListClickListener() {
@Override
public void onAddressClicked(Pojo item) {
// Show map view based on item's address
}
@Override
public void onPhoneClicked(Pojo item) {
// Call number of item that was clicked
}
};
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList, listener);
答案 3 :(得分:0)
为PhoneView和addressView添加View.Onclicklistener
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
/****your code*****/
//add listener
addressView.setOnClickListener(mClickListener);
phoneView.setOnClickListener(mClickListener);
return listView;
}
//add View.Onclicklistener for phoneView and addressView
private OnClickListener mClickListener;
public void setOptionClickListener(OnClickListener listener) {
this.mClickListener = listener;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
//when you click list's item
pojo currentTarget;
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
currentTarget = pojo
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
adapter.setOptionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.location_data:
//TODO: currentTarget's data that opens up a map
break;
case R.id.phone_data:
//TODO: currentTarget's data that opens up the phone
break;
}
}
});
return rootView;
}