Java 一个ArrayList项上的多个意图
我有一个ArrayList,其中包含一些酒店的详细信息,我想做的是单击地址打开地图,然后单击电话号码打开电话 任何帮助都将不胜感激。 这些是代码片段Java 一个ArrayList项上的多个意图,java,android,arrays,android-intent,android-support-design,Java,Android,Arrays,Android Intent,Android Support Design,我有一个ArrayList,其中包含一些酒店的详细信息,我想做的是单击地址打开地图,然后单击电话号码打开电话 任何帮助都将不胜感激。 这些是代码片段 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = i
@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;
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//为该碎片膨胀布局
视图根视图=充气机。充气(R.layout.item_列表,容器,错误);
final ArrayList pojoArrayList=新的ArrayList();
添加(新的pojo(getString(R.string.hotel\u one)、R.drawable.hotel\u one、R.drawable.gradient\u splash、R.drawable.ic\u map\u、getString(R.string.hotel\u one\u location)、R.drawable.ic\u phone\u solid、getString(R.string.hotel\u one\u phone));
pojoArrayList.add(新的pojo(getString(R.string.hotel\u two)、R.drawable.hotel\u tow、R.drawable.gradient\u splash、R.drawable.ic\u map\u、getString(R.string.hotel\u two\u位置)、R.drawable.ic\u phone\u solid、getString(R.string.hotel\u two\u phone));
pojoArrayList.add(新的pojo(getString(R.string.hotel\u three)、R.drawable.hotel\u three、R.drawable.gradient\u splash、R.drawable.ic\u map\u、getString(R.string.hotel\u three\u location)、R.drawable.ic\u phone\u solid、getString(R.string.hotel\u three\u phone));
添加(新的pojo(getString(R.string.hotel\u four)、R.drawable.hotel\u four、R.drawable.gradient\u splash、R.drawable.ic\u map\u、getString(R.string.hotel\u four位置)、R.drawable.ic\u phone\u solid、getString(R.string.hotel\u four\u phone));
pojoArrayList.add(新的pojo(getString(R.string.hotel\u-five)、R.drawable.hotel\u-five、R.drawable.gradient\u splash、R.drawable.ic\u map\u、getString(R.string.hotel\u-five\u位置)、R.drawable.ic\u-phone\u solid、getString(R.string.hotel\u-five\u-phone));
HotelsAdapter=新的HotelsAdapter(getActivity(),pojoArrayList);
ListView listViewItems=(ListView)rootView.findViewById(R.id.list);
setAdapter(适配器);
listViewItems.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
pojo pojo=pojoArrayList.get(位置);
如果(位置==0){
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(“电话:+31202246280”);
星触觉;
}否则如果(位置==1){
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(“电话:+31205063715”);
星触觉;
}否则如果(位置==2){
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(“电话:+31208812595”);
星触觉;
}否则如果(位置==3){
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(“电话:+31206651171”);
星触觉;
}否则{
意向callIntent=新意向(意向.行动拨号);
setData(Uri.parse(“电话:+3120103535”);
星触觉;
}
}
});
返回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;
}
}
公共类HotelAdapter扩展了ArrayAdapter{
公共HotelAdapter(@NonNull上下文,ArrayList pojo){
super(上下文,0,pojo);
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
查看列表视图=转换视图;
如果(listView==null){
listView=LayoutInflater.from(getContext()).flate(R.layout.hotel_items,父项,false);
}
pojo currentItem=getItem(位置);
ImageView背景图像=(ImageView)listView.findViewById(R.id.category\u图像);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader=(TextView)listView.findViewById(R.id.category\u头);
categoryHeader.setText(currentItem.getHeaderResource());
视图类别梯度=(视图)listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView位置图标=(ImageView)listView.findViewById(R.id.location_图标);
setImageResource(currentItem.getLocationIconID());
TextView addressView=(TextView)listView.findViewById(R.id.location\u数据);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon=(ImageView)listView.findViewById(R.id.phone_图标);
setImageResource(currentItem.getPhoneIconID());
TextView phoneView=(TextView)listView.findViewById(R.id.phone_数据);
setText(currentItem.getPhoneID());
返回列表视图;
}
}
到现在为止,我只有一个意图,那就是打开拨号器。如何分别选择位置部分和电话部分,并对这两个部分有不同的意图?试试这个 首先,我建议使用Recylerview,因为Recylerview是高级的 请点击此处:
listViewItems.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
pojo pojo=pojoArrayList.get(位置);
LinearLayout linear\u phone=view.findViewById(R.id.phone\u布局);
linear\u phone.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共voi
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
}
});
}
});
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;
}
}
public interface HotelListClickListener {
void onAddressClicked(Pojo item);
void onPhoneClicked(Pojo item);
...
}
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);
}
});
...
}
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);
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;
}