I have an ArrayList that contains details of a few hotels, what i want to do is on clicking at the address it opens up a map intent and on clicking on the phone number it opens up the phone.
Any help would be much appreciated. These are the code snippets.
@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;
}
And this is the layout for it:
<?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>
The adapter:
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;
}
}
As of now I just have one intent that opens up the dialer. How would I select the location section and the phone section separately and have separate intents for both?
Try this,
and remove listview.onitemClicklistener();