0

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):

enter image description here

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Ahmed
  • 33
  • 6

1 Answers1

0

The following steps should help you solve this if I got your question correctly:

  1. Add another field to Cell to represent the location URI.
  2. Get this new field and use it to set the currently hardcoded value. e.g mapIntent.setData(currentNumber.getLocationURI());
  3. Then, when creating the Cell objects, you need to pass in the location URI like you did for its other fields in the Cell constructor.
Azeez Olaniran
  • 625
  • 1
  • 6
  • 11
  • Thanks azeez for the reply. In this method I'd be using the location link from google maps for instance, right ? I'm trying it and it looks promising. but is there an easy way to use lat&long. ? – Ahmed Mar 19 '18 at 05:03
  • I'm not sure I understand what you mean by an easier way. But: - What do the location links from google maps look like? can you share an example. Maybe you could just parse the Lat and Long from the String? - If you have access to the Location object, then the Longitude and Latitude will be as easy as in this question https://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobile-device-in-android – Azeez Olaniran Mar 19 '18 at 10:14
  • what I did is adding a String field in my Cell class and put the google map link as an argument (example:www.google.com/maps/place/...) So when the view is clicked, mapIntent open this link. I'm wondering if there is a way for a beginner if I need to use lang and lat data :) ? – Ahmed Mar 19 '18 at 12:56