9

I have requirement to show list of some places in Android ListView with the direction of that place. I able to that by finding the angle between the locations but the issue is that My Client required the arrow to be rotated when the device was rotated or moved. I failed to found any solution that will rotate arrow as per Sensor Changes. Do any of them across such requirement? Please suggest me some solution to get the list to be displayed as this in this iPhone App. or look at below image.

UPDATED:

I am expecting any answer such that it will be helpful in starting the below requirement. Even displaying the arrow alone is welcome so that I will try to achieve the below. all the samples I have done are shaking and they are not stable.

enter image description here

3
  • With what part do you have problems? You have problems getting the sensor's directions or with showing the arrows changing according to the sensors in the ListView?
    – user
    Feb 5, 2013 at 19:13
  • I have implemented how to get direction from two different location values and calculated the angle without using sensor. Now I required that to be applied for all the items in the listview as shown in above pic. I also implemented with sensor in which it showed correctly initially and when moved the device, problem is started as it doesn't stay stable and never come to a stable position and always shakes into wrong directions
    – TNR
    Feb 6, 2013 at 6:36
  • Without seeing your code(for the ListView's adapter and the sensor change listener) nobody can really help you.
    – user
    Feb 6, 2013 at 20:59

2 Answers 2

2
+50

Have your adapter hold a collection of objects with all info you want on each list item, including a float value for rotation of arrow.

public class Place {
 private Srting name;
 private double distance;
 private float arrowAngle;

//-- etc etc--

Override Adapter's getView(), set rotation of arrow View:

@Override
public View getView(View currentView, int position, ViewGroup parent){
  //--inflate layout for each row, find views, set values etc etc--

  Place p = getItem(position);
  arrowView.setRotation(p.getArrowAngle());

 //--return inflated view --
}

On sensor change, calculate new angle of each arrow, and update each of the data objects in your adapter.

for (int i = 0 ; i < adapter.getCount(); i++){
    Place p = adapter.getItem(i);

   //--calculate angle--

   p.setArrowAngle(newAngle);
}

adapter.notifyDataSetChanged();

That would do it, but sensors may change very fast and too much updates of ListView data will cause ListView to lag, to limit updates, declare a long variable lastUpdated, On sensor change see if System.nanoTime() - lastupdated > 500, then only call the above updating code. This will limit updates to a 500 millisecond interval.

2

In your sensor change event handler, call ListView.invalidateViews(). This will cause all of the existing views in the ListView to redraw. SO, if you are currently to the point where you can draw the list correctly and get events from the sensors as you like, then adding this call should get you the behavior you want.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.