安卓:指南针+在ListView距离 [英] Android: compass + distance in a listview

查看:129
本文介绍了安卓:指南针+在ListView距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想你在地图的所有尝试谷歌的地方。 这是POI接近你的清单。

I guess you have all tried "Google Places" in Maps. This is a list of POI close to you.

我真的想这样做同样的功能,在我的应用程序的GPS坐标列表,但似乎非常复杂。

I really would like to do the same feature in my application with a list of GPS coordinates, but that seem really complicated.

制作与距离和小箭头的列表视图是很容易的,但我不明白如何更新列表和箭头每次用户移动他的电话。

Making the listview with the distance and the little arrow is very easy, but I cannot understand how to update this list and the arrow everytime the user move his phone.

现在我有一个静态列表视图。

For now I have a static listview.

我想知道,如果有人成功创建这样的列表视图。

I would like to know if someone succeed to create such a listview.

感谢很多的任何信息。

推荐答案

有几个选择你想要达到的目标。就个人而言,我建议你实现自己的适配器(最有可能在这种情况下,通过扩展SimpleCursorAdapter)和封装的更新范围内的距离文字和指南针方向旋转。

There are a few options for what you're trying to achieve. Personally, I'd recommend implementing your own Adapter (most likely in this case by extending SimpleCursorAdapter) and encapsulating the updates to distance text and compass heading rotation within that.

要帮助资源管理,你可能想将承载ListView的活动中创建一个SensorListener和LocationListener的。每当你收到一个更新打电话给你的自我滚动从你的适配器类中updateCompassAndLocation 方法。

To help with resource management, you probably want to create a SensorListener and LocationListener within the Activity that will host the ListView. Whenever you receive an update call your self-rolled updateCompassAndLocation method from within your Adapter class.

从这个方法中,你有两个选择。无论是遍历每个组成数据收集正在重新presented的项目并修改指南针图形和距离的文字,或简单地记录当前位置和航向为变量的类中,并调用 notifyDataSetChanged 强制适配器更新 getView 方法中本身的意见。在这两种情况下(尤其是后者),你需要在getView设定的距离文本和标题指南针值。

From within that method you have two options. Either iterate over each of the items that make up the data collection being represented and modify the compass graphic and distance text, or simply record the current location and heading as variables within the class, and call notifyDataSetChanged to force the adapter to update the views itself within the getView method. In either case (especially the latter), you'll need to set the distance text and heading compass values within getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  LinearLayout myView;

  MyPOI item = getItem(position);

  Location poiLocation = item.getLocation;

  int compassHeading = // Calculate heading relative to current heading
  float distance = // Calculate distance to POI


  if (convertView == null) {
    myView = new LinearLayout(getContext());
    String inflater = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
    vi.inflate(resource, myView, true);
  } else {
    trainView = (LinearLayout) convertView;
  }

  TextView distanceView = (TextView)trainView.findViewById(R.id.distance);
  ImageView compassView = (ImageView)trainView.findViewById(R.id.compass);

  distanceView.setText(String.valueOf(distance);
  compassView.setImageLevel(compassHeading);
}

这篇关于安卓:指南针+在ListView距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆