I'm developing my first App ever, but I'm facing a problem that I cannot overcome. I'm creating a ListView for restaurants in which (Name, Phone, Logo, and Location) for each one will be shown in its List Item.
Everything is going well except for the coordination. I was able only to hardcode the coordinates in getview method, but I don't know how to change it along with the arraylist.
Is there a way to add it to the arraylist as another Arguement?
Any help will be much appreciated :)
These are my codes in which I just added a string for the location and Intent to open the fixed coordinates.
for the new object class:
public class Cell {
private String mName;
private String mPhone;
private int mLogo;
private String mLocation;
public Cell (String name, String phone, int logo, String location){
mName = name;
mPhone = phone;
mLogo = logo;
mLocation = location;}
public String getmName() {return mName;}
public String getmPhone(){return mPhone;}
public int getmLogo(){return mLogo;}
public String getmLocation() {return mLocation;}
this for the customAdapter:
public class CellAdapter extends ArrayAdapter<Cell> {
private static final String LOG_TAG = CellAdapter.class.getSimpleName();
public CellAdapter(Activity context, ArrayList<Cell> restaurant) {
super(context, 0, restaurant);
}
@NonNull
@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);
}
final Cell currentNumber = getItem(position);
TextView mapView = (TextView) listItemView.findViewById(R.id.map_view);
mapView.setText(currentNumber.getmLocation());
mapView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(Uri.parse("geo:47.6, -122.3"));
getContext().startActivity(mapIntent);
}
});
TextView nameView = (TextView) listItemView.findViewById(R.id.name_view);
nameView.setText(currentNumber.getmName());
TextView phoneView = (TextView) listItemView.findViewById(R.id.phone_view);
phoneView.setText(currentNumber.getmPhone());
phoneView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel: " + currentNumber.getmPhone()));
getContext().startActivity(call);
}
});
ImageView logoView = (ImageView) listItemView.findViewById(R.id.logo_view);
logoView.setImageResource(currentNumber.getmLogo());
return listItemView;
}
and this is the activity code:
public class RestaurantsActivity extends AppCompatActivity {Intent mapIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurants);
final ArrayList<Cell> restaurant = new ArrayList<>();{
restaurant.add(new Cell("Restaurant 1", "12345", R.drawable.logo_1, "MAP"));
restaurant.add(new Cell("Restaurant 2", "67890", R.drawable.logo_2, "MAP"));
CellAdapter restaurantAdapter = new CellAdapter(this, restaurant);
ListView restaurantView = (ListView) findViewById(R.id.restaurant_view);
restaurantView.setAdapter(restaurantAdapter);
}
}
}
The final result I need is to update the data in mapIntent with different restaurants when the user click (MAP):