0

ListView with ArrayAdapter seems to be the only way of binding data to UI elements, and having access to rendering/rerendering strategies.

I want to absolutely position (x,y) UI elements (markers) above an image (map). If the data were to remain static, I would simply use ImageView within RelativeLayout within FrameLayout and set margins of the RelativeLayout to change the x and y position of the markers ("simply" is a gross understatement).

However, the data will change at intervals (the markers will move or change color), and I was hoping to leverage ArrayAdapter's notifyDataSetChanged() method to rerender the markers and reposition them.

Two questions (possibly unrelated to each other):

  1. Is ArrayAdapter with ListView the only way to bind (when data changes, UI elements rerender) data to UI elements in Android?
  2. Can I absolute position (think HTML/CSS) UI elements that are Items in a ListView?

EDIT I have something implemented now that "works" ...

    // Excerpt from MyCustomAdapter.getView()
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(50, 50);
    convertView.setLayoutParams(layoutParams);
    convertView.setY((float) y);
    convertView.setX((float) x);
    return convertView;

... I do not like this solution because the convertView views (markers on a map) jump around before they end up in the correct position (not index, but x, y position).

EDIT 2

    int pullUpPullOver = 25; // half the height/width of the marker (to center it) ...
    int offsetTop = 50 * position; // each item pushes the next down by 50 ...
    int x = (location.getX() - pullUpPullOver);
    int y = (location.getY() - pullUpPullOver) - offsetTop;

    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(50, 50);

    convertView.setLayoutParams(layoutParams);
    convertView.setTranslationX((float) x);
    convertView.setTranslationY((float) y);

I don't like the sad hacks to do this. With HTML/CSS absolute and relative positioning in mind, is there a standard way for absolutely positioning elements (within a container view) in Android?

Mark Peterson
  • 570
  • 5
  • 19

2 Answers2

1

No arrayadapter is not the only way to bind UI elements when data changes. Changing UI content is very simple Java. Take the example of a TextView.

public TextView tv;
//on your oncreate method
tv = (TextView) findViewById(R.id.mytextview);
//now your own function
public void onDataChange(String str){
tv.setText(str);
}
//you can call this function after you detect that data changed

Yes you can absolute position UI elements of a ListView but it is not the recommended practice Please visit this link Design Guides

BenMorel
  • 34,448
  • 50
  • 182
  • 322
subash
  • 172
  • 1
  • 2
  • 9
0

I found the answer to my question here Android AdapterView?

Instead of using ListView, create your own class extending AdapterView in a similar manner as described here (remove the methods you/I don't need) http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/

Community
  • 1
  • 1
Mark Peterson
  • 570
  • 5
  • 19