【问题标题】:How to add coordinates in a TextView for each list item of the ListView如何在 TextView 中为 ListView 的每个列表项添加坐标
【发布时间】:2018-03-22 01:49:12
【问题描述】:

我正在开发我的第一个应用程序,但我遇到了一个我无法克服的问题。 我正在为餐厅创建一个 ListView,其中每个餐厅的(名称、电话、徽标和位置)都将显示在其列表项中。

一切都很顺利,除了协调。 我只能在 getview 方法中硬编码坐标,但我不知道如何将它与 arraylist 一起更改。

有没有办法将它作为另一个参数添加到数组列表中?

任何帮助将不胜感激:)

这些是我的代码,我刚刚在其中添加了位置字符串和打开固定坐标的意图。

对于新的对象类:

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;}

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;

}

这是活动代码:

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

            }
        }

    }

我需要的最终结果是当用户点击(MAP)时,用不同的餐厅更新mapIntent中的数据:

【问题讨论】:

  • 你能分享一张你需要的布局图片和另一张出现在你面前的图片吗
  • 我更新了帖子,因为我在试用期间发现了一些错误:)
  • 我在话题末尾添加了截图链接

标签: java android listview coordinates


【解决方案1】:

如果我的问题正确,以下步骤应该可以帮助您解决这个问题:

  1. 向 Cell 添加另一个字段以表示位置 URI。
  2. 获取这个新字段并使用它来设置当前的硬编码值。例如mapIntent.setData(currentNumber.getLocationURI());
  3. 然后,在创建 Cell 对象时,您需要像在 Cell 构造函数中为其他字段所做的那样传递位置 URI。

【讨论】:

  • 感谢 azeez 的回复。例如,在这种方法中,我会使用来自谷歌地图的位置链接,对吗?我正在尝试,它看起来很有希望。但是有没有一种简单的方法来使用纬度和经度。 ?
  • 我不确定我是否理解您所说的更简单的方法。但是: - 来自谷歌地图的位置链接是什么样的?你能分享一个例子吗?也许你可以从字符串中解析 Lat 和 Long? - 如果您有权访问 Location 对象,那么经度和纬度将像这个问题一样简单stackoverflow.com/questions/2227292/…
  • 我所做的是在我的 Cell 类中添加一个字符串字段并将谷歌地图链接作为参数(例如:www.google.com/maps/place/...)所以当视图点击后,mapIntent 打开这个链接。我想知道如果我需要使用 lang 和 lat 数据,是否有适合初学者的方法:)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多