Nightfellas
Nightfellas

Reputation: 1

How to uniquely identify a View

I'm new to Java and android programming stuff, and I'm sorry if my english is bad :D

I am making an app with drag and drop feature. I'm using the RelativeLayout for the drop zone and a generated ImageView as the item.

the data came from the ListView. when I drag the ListView item and drop it on the relativeLayout, it will create an ImageView. but How can I "insert" the value from the ListView item into an ImageView, so I can check the existing data when I drop the new item on the layout so the same data from the listview cannot be dropped.

here's my code on ACTION_DROP

ClipData.Item item = event.getClipData().getItemAt(0);// I can get the dropped Item data from this
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
final RelativeLayout root = (RelativeLayout)findViewById(R.id.relLay);
final ImageView img = new ImageView(getApplicationContext());
img.setMinimumWidth(32);
img.setMaxWidth(32);
img.setMinimumHeight(32);
img.setMaxHeight(32);
img.setBackgroundResource(R.drawable.marker2);
img.setX(x_cord);
img.setY(y_cord);
root.addView(img);

Upvotes: 0

Views: 874

Answers (1)

Caner
Caner

Reputation: 59148

Create a unique ID for each view you create so you can distinguish.

Object tag = ... // Create a unique `tag` object depending on the data that comes from `ListView`

ImageView existingView = root.findViewWithTag(tag);
if (existingView == null) { // Didn't exist
    ImageView newView = new ImageView(getApplicationContext());
    newView.setTag(ID);
    root.addView(newView);
}

Upvotes: 1

Related Questions