0

I am trying to call a drag listener on convertView.

I am not accessing the longclick listener or the drag listener with the above code. I longclick on the image that is in gridview and the image shows a blue background that is slightly bigger than the image, but the image does not drag to a different location in the gridview.

JohnWilliams
  • 139
  • 1
  • 13
  • where have you initialized myDragListener? In which method because I do not see it inside getView()? – Kaixin Aug 17 '15 at 16:14
  • I provide all of my code. So, I must initialize myDragListener? Can you please provide more of an explanation? Thanks. – JohnWilliams Aug 17 '15 at 16:56

1 Answers1

1

"Variable 'myDragListener' is never used" hints that myDragListener is either not inside the same Java class (in your case - your custom adapter), or is not accessible (for example if myDragListener was declared public static in another class, you would be able to access it).

To solve it, you'll have to make myDragListener accessible, or instead, make your adapter implement View.OnDragListener itself, like in the following example:

public class ImageAdapter extends BaseAdapter implements View.OnDragListener{ //Edited

    Context context;
    LayoutInflater inflater;


    public MyAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

And the getView() function:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.activity_column, null);
        viewHolder = new ViewHolder((ImageView) convertView.findViewById(R.id.ColPhoto));
        convertView.setTag(viewHolder);
        convertView.setOnDragListener(this); //Edited
    } else
        viewHolder = (ViewHolder) convertView.getTag();

    viewHolder.iv.setImageBitmap((Bitmap) array.get(position).get(TAG_IMG));
    viewHolder.position = position;
    return convertView;
}

@Override
public boolean onDrag(View v, DragEvent event) {
    Log.v("draglistener", "draglistener");
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
        case DragEvent.ACTION_DRAG_ENTERED:
        case DragEvent.ACTION_DRAG_EXITED:
        case DragEvent.ACTION_DRAG_LOCATION:
        case DragEvent.ACTION_DRAG_ENDED:
            return true;
        case DragEvent.ACTION_DROP:
            //imageAdapter.addNewImage(v, event);
            break;
        default:
            break;
    }
    return true;
}

Please note that instead of calling getSystemService, it's preferable to pass Context as a parameter to the adapter's constructor, and then define the inflater once, via inflater = LayoutInflater.from(context).

Also note that the call .setOnDragListener() requires API Level 11 and above.

Sahar Avr
  • 1,168
  • 9
  • 10
  • Thank you for your response. I followed your code, but also implemented the one line you commented out as it greatly pertains to what I am doing. However, using your code, I do not receive errors, but the drag feature does not work anymore. To clarify, when I long click on an image and attempt to drag, the image does not move. Any suggestions? – JohnWilliams Aug 17 '15 at 16:49
  • That's another question. Have you tried following this discussion: http://stackoverflow.com/questions/28533762/drag-and-drop-for-imageview-not-working ? – Sahar Avr Aug 17 '15 at 16:58
  • With all respect and after looking at that discussion, sir, I am not certain that it is another question because using your code, I never access the drag capability whatsoever. The log I have inside the ondrag is never printed the log cat. – JohnWilliams Aug 17 '15 at 17:12
  • I completely understand your point. Let me offer another solution- inside your `getView()`, replace `convertView.setOnDragListener(myDragListener)` with `convertView.setOnDragListener(this)` . Then hit `Ctrl+Enter` to make the adapter **implement** the OnDragListener method. Tell me if it resulted in any changes. – Sahar Avr Aug 17 '15 at 17:35
  • Variable 'myDragListener' is never used @ View.OnDragListener myDragListener = new View.OnDragListener() – JohnWilliams Aug 17 '15 at 17:47
  • Did you remove all the references to myDragListener? If you implement View.OnDragListener you don't need myDragListener Also, can you share more of your code? I want to try to better help you – Sahar Avr Aug 17 '15 at 18:00
  • How will I access the drag listener with convertView in getView if I remove myDragListener? – JohnWilliams Aug 17 '15 at 18:04
  • Thank you for your time and help so far. gridV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() and onDrag are never logged. – JohnWilliams Aug 17 '15 at 18:56
  • Have you changed the way you inflate convertView to the way I showed above? – Sahar Avr Aug 17 '15 at 19:06
  • I refreshed the page, but I still see that in your code, ImageAdapter does not implement `OnDragListener`, and you still use `(LayoutInflater) getSystemService` instead of declaring the inflater in the constructor and then call `inflater.inflate(...)` in getView . Change these according to the code in my edited answer and tell me what happens – Sahar Avr Aug 17 '15 at 20:02
  • Sorry, sir. Now please see above. gridV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() and onDrag are never logged. – JohnWilliams Aug 17 '15 at 20:10
  • change `convertView = ((LayoutInflater) getSystemService...` to `convertView = inflater.inflate(R.layout.activity_column, null);` – Sahar Avr Aug 17 '15 at 20:12
  • I changed that line of code and still encounter the same issue. – JohnWilliams Aug 17 '15 at 20:15
  • It appears that the listeners are on the wrong view, but getCount() is on the correct view. Any suggestions, please and thank? – JohnWilliams Aug 17 '15 at 20:28
  • I tried copying the exact code to a new project and it seems to work. I'm trying to think of another reason why your listeners won't log. If you have any more information that can be useful for that matter, please share it – Sahar Avr Aug 17 '15 at 22:45
  • Can you please tell me what view getCount() takes? – JohnWilliams Aug 18 '15 at 15:32
  • `getCount()` should return the size of your adapter's data. For example if your adapter's data set is an `ArrayList`, then `getCount()` should return `ArrayList.size()` – Sahar Avr Aug 21 '15 at 01:39