OnItemSelectedListener in drag and drop one Listview to another ListView?
<p>I am preparing Drag and Drop app.I got sample app from some where.I made some changes in that code for my requirement.It's working fine up to 4th image of ListView.In my view By default ListView display up to 3rd position(means 0,1,2,3),if i drag the 2nd position from first ListView and drop in second ListView,It's  display same image.When i scroll down,positions is 4,5,6,7.if i drag 6th position,it's takes 2nd position.My code is,</p>

<pre><code>public class MyDragAndDropActivity extends Activity {

    private RelativeLayout mainRelativeLayout;
    private ListViewDragDrop lvPicture;
    private ListViewDragDrop lvRecever;
    private ItemAdapter itemAdapter;
    private ReceverAdapter receverAdapter;
    private ImageView imageDrag;
    private OneItem oneItemSelected;
    private Context context;
    int POSITION, mPositionFlag = 0;

    // ArrayList mArryLstPosition = new ArrayList();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;

        mainRelativeLayout = (RelativeLayout) RelativeLayout.inflate(context,
                R.layout.main_list, null);
        lvPicture = (ListViewDragDrop) mainRelativeLayout
                .findViewById(R.id.listView1);
        lvRecever = (ListViewDragDrop) mainRelativeLayout
                .findViewById(R.id.listView2);
        imageDrag = (ImageView) mainRelativeLayout
                .findViewById(R.id.imageView1);

        itemAdapter = new ItemAdapter(context, constructList());
        receverAdapter = new ReceverAdapter(context, new ArrayList&lt;OneItem&gt;());

        lvPicture.setAdapter(itemAdapter);
        lvRecever.setAdapter(receverAdapter);

        /*
         * lvPicture.setOnItemClickListener(new OnItemClickListener() {
         * 
         * @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int
         * position, long id) { // TODO Auto-generated method stub
         * 
         * 
         * POSITION=position; mArryLstPosition.add(POSITION);
         * 
         * Log.d("position is -----",""+POSITION);
         * 
         * i=i+1;
         * 
         * mPositionFlag=1; } });
         */

        /**
         * Set selected Listener to know what item must be drag Save selected
         * item
         */
        lvPicture.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView&lt;?&gt; arg0, View view,
                    int position, long id) {
                // TODO Auto-generated method stub+
                oneItemSelected = (OneItem) arg0.getItemAtPosition(position);
                imageDrag.setImageDrawable(context.getResources().getDrawable(
                        oneItemSelected.getId()));
            }

            @Override
            public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                // TODO Auto-generated method stub
            }
        });
        /**
         * Set an touch listener to know what is the position when item are move
         * out of the listview
         */
        lvPicture.setOnItemMoveListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                RelativeLayout.LayoutParams layout = (RelativeLayout.LayoutParams) imageDrag
                        .getLayoutParams();
                imageDrag.setVisibility(ImageView.VISIBLE);
                System.out.println("in on Item move listener");
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    imageDrag.bringToFront();
                    // System.out.println("in on Item move listener--down");
                    return true;
                }

                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    layout.leftMargin = (int) event.getX()
                            - imageDrag.getWidth() / 2;
                    layout.topMargin = (int) event.getY()
                            - imageDrag.getHeight() / 2;
                    // System.out.println("in on Item move listener--move");
                }

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    imageDrag.setVisibility(View.GONE);
                    // System.out.println("in on Item move listener---up");
                }

                imageDrag.setLayoutParams(layout);

                return true;
            }
        });

        /**
         * Listener to know if the item is droped out of this origin ListView
         */
        lvPicture.setOnItemUpOutListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // System.out.println("in on m On Item UpOut Listener");
                return lvRecever.onUpReceive(event);
            }
        });

        /**
         * Listener to know on what position the new item must be insert
         */
        lvRecever.setOnItemReceiverListener(new OnItemClickListener() {

            public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                    int position, long arg3) {
                if (oneItemSelected != null)
                    receverAdapter.addPicture(oneItemSelected, position);
                System.out.println("in on listener Receive Picture--arg2---"
                        + position);

            }
        });

        /**
         * An listemer to remove an item
         */
        lvRecever.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
                    int arg2, long arg3) {
                receverAdapter.removeItem(arg2);
                // System.out.println("in on listener Remove Item--down");
                return false;
            }
        });

        setContentView(mainRelativeLayout);
    }

    private ArrayList&lt;OneItem&gt; constructList() {
        ArrayList&lt;OneItem&gt; al = new ArrayList&lt;OneItem&gt;();

        OneItem op = new OneItem(R.drawable.cream2, "cream2");
        al.add(op);

        OneItem op2 = new OneItem(R.drawable.cream3, "cream3");
        al.add(op2);

        OneItem op3 = new OneItem(R.drawable.cream4, "cream4");
        al.add(op3);

        OneItem op4 = new OneItem(R.drawable.cream6, "cream6");
        al.add(op4);

        OneItem op6 = new OneItem(R.drawable.salad_a, "salad_a");
        al.add(op6);

        OneItem op7 = new OneItem(R.drawable.salad_b, "salad_b");
        al.add(op7);

        OneItem op8 = new OneItem(R.drawable.salad_c, "salad_c");
        al.add(op8);

        OneItem op9 = new OneItem(R.drawable.salad_e, "salad_e");
        al.add(op9);

        OneItem op10 = new OneItem(R.drawable.salad_f, "salad_f");
        al.add(op10);

        OneItem op5 = new OneItem(R.drawable.salad_j, "salad_j");
        al.add(op5);

        return al;
    }

}
</code></pre>

<p>Please help me.</p>

<p>this ListView class, </p>

<pre><code> public class ListViewDragDrop extends ListView{

        private OnTouchListener mOnItemOutUpListener;
        private OnTouchListener mOnItemMoveListener; //It is an hacked touchLister, in fact it is OnMoveListener
        private OnItemClickListener mOnItemReceiver;
        private boolean isMove = false;
        private View childSelected;
        private float xInit;
        private float yInit;


        public ListViewDragDrop(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        public ListViewDragDrop(Context context, AttributeSet attrs) {
           super(context, attrs);
      }

       public ListViewDragDrop(Context context, AttributeSet attrs, int defStyle) {
             super(context, attrs, defStyle);
       }

       /**
        * When an item is moved horizontally out of this position, this listener is called. Before this OnItemSelectedListener is called.
        * @param listener
        */
        public void setOnItemMoveListener(AdapterView.OnTouchListener listener){
            mOnItemMoveListener = listener;
        }

        /**
         * When a gesture on a item is terminated out of the the listView
         * @param listener
         */
        public void setOnItemUpOutListener(AdapterView.OnTouchListener listener){
            this.mOnItemOutUpListener = listener;
        }

        /**
         * When an outsider item is moved and up on this listview. Return position where to add this item.
         * @param listener
         */
        public void setOnItemReceiverListener(AdapterView.OnItemClickListener listener){
            this.mOnItemReceiver = listener;
        }



       @Override
       public boolean onTouchEvent(MotionEvent ev) {
           boolean handled = false;

            if(mOnItemMoveListener != null &amp;&amp; !handled)
                handled = onMove(ev);

            if(!handled)
           return super.onTouchEvent(ev);

        return handled;
       }

       /**
        * 
        * @param e
        * @return
        */
       public boolean onUpReceive(MotionEvent e) {

            if(e.getAction() == MotionEvent.ACTION_UP){

                int x = (int)e.getX();
                int y = (int)e.getY();

                for(int i=0;i&lt;getChildCount();i++){
                    Rect viewRect = new Rect();
                    View child = getChildAt(i);
                    int left = child.getLeft() + this.getLeft();
                    int right = child.getRight() + this.getLeft();
                    int top = child.getTop() + this.getTop();
                    int bottom = child.getTop() + child.getHeight()/2 + this.getTop();

                    viewRect.set(left, top, right, bottom);

                    if(viewRect.contains(x,y)){
                        if(getOnItemSelectedListener() != null){
                            getOnItemSelectedListener().onItemSelected(ListViewDragDrop.this, child, i, getItemIdAtPosition(i));

                        }
                        if(mOnItemReceiver != null){
                            mOnItemReceiver.onItemClick(ListViewDragDrop.this, child, i, getItemIdAtPosition(i));
                        }
                        return true;
                    }

                    Rect viewRect2 = new Rect();
                    left = child.getLeft() + this.getLeft();
                    right = child.getRight() + this.getLeft();
                    top = child.getTop()  + child.getHeight()/2 + this.getTop();
                    bottom = child.getBottom() + this.getTop();
                    viewRect2.set(left, top, right, bottom);


                    if(viewRect2.contains(x,y)){
                        if(getOnItemSelectedListener() != null){
                            getOnItemSelectedListener().onItemSelected(ListViewDragDrop.this, child, i, getItemIdAtPosition(i));
                        }
                        if(mOnItemReceiver != null){
                            mOnItemReceiver.onItemClick(ListViewDragDrop.this, child, i + 1, getItemIdAtPosition(i));
                        }
                        return true;
                    }

                }

                int left = this.getLeft();
                int right = this.getRight();
                int top = this.getTop();
                int bottom = this.getBottom();
                Rect rect = new Rect(left, top, right, bottom);

                if(rect.contains(x,y)){

                    if(this.getChildCount() &gt; 0){
                        int  minY = this.getChildAt(this.getChildCount() - 1).getBottom();
                        int maxY = this.getChildAt(0).getTop();

                        if(y &lt; minY){
                            mOnItemReceiver.onItemClick(ListViewDragDrop.this, null, 0, 0);
                        }else{
                            if(y &gt; maxY){
                                mOnItemReceiver.onItemClick(ListViewDragDrop.this, null, this.getChildCount() , 0);
                            }

                        }

                        return true;

                    }else{

                        if(mOnItemReceiver != null){
                            mOnItemReceiver.onItemClick(ListViewDragDrop.this, null, 0, 0);
                        }
                        return true;
                    }

                }


            }
            return false;
        }

        public boolean onMove(MotionEvent event) {

            float xNow = event.getX();
            float yNow = event.getY();
            //System.out.println("xNow is----"+xNow);
            //System.out.println("yNow is----"+yNow);
            Rect viewRect = new Rect();

            if(event.getAction() == MotionEvent.ACTION_DOWN){
                float xInit1 = event.getX();
                float yInit1 = event.getY();

                for(int i=0;i&lt;getChildCount();i++){
                    View child = getChildAt(i);
                    int left = child.getLeft();
                    int right = child.getRight();
                    int top = child.getTop();
                    int bottom = child.getBottom();
                    viewRect.set(left, top, right, bottom);
                    if(viewRect.contains((int)xInit1, (int)yInit1)){

                        if(getOnItemSelectedListener() != null){
                            getOnItemSelectedListener().onItemSelected(ListViewDragDrop.this, child, i, getItemIdAtPosition(i));
                        }

                        this.xInit = xInit1;
                        this.yInit = yInit1;

                        childSelected = child;


                    }
                }
            }

            if(event.getAction() == MotionEvent.ACTION_MOVE){

                if(!isMove){

                    if( Math.abs(xNow -xInit) &gt; 0 &amp;&amp; Math.abs(yNow - yInit) &lt; 4){

                        if(mOnItemMoveListener != null){
                            mOnItemMoveListener.onTouch(ListViewDragDrop.this, event);
                        }

                        isMove = true;
                        return true;

                    }

                }else{
                    mOnItemMoveListener.onTouch(ListViewDragDrop.this, event);

                    return true;
                }
            }

            if(event.getAction() == MotionEvent.ACTION_UP &amp;&amp; isMove){
                int left = this.getLeft();
                int right = this.getRight();
                int top = this.getTop();
                int bottom = this.getBottom();
                Rect rect = new Rect(left, top, right, bottom);

                if(mOnItemMoveListener != null){
                    mOnItemMoveListener.onTouch(ListViewDragDrop.this, event);
                }

                if(!rect.contains((int)xNow, (int)yNow)){

                    if(mOnItemOutUpListener != null){
                        mOnItemOutUpListener.onTouch(this.childSelected, event);
                    }

                }

                isMove = false;
                return false;
            }

            return false;
        }


    }
</code></pre>

<p>this adapter class,</p>

<pre><code>public class ItemAdapter extends BaseAdapter {

    private Context context;
    public static ArrayList&lt;OneItem&gt; alPicture;
    static ArrayList mArryPositions = new ArrayList();
    int mChangePositions;

    int lastValueInmArryPositions, firstValueInmArryChangePositions, i, k, j;

    public ItemAdapter(Context context, ArrayList&lt;OneItem&gt; alPicture) {
        this.context = context;
        this.alPicture = alPicture;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // System.out.println("size of array "+alPicture.size());
        return alPicture.size();
    }

    @Override
    public OneItem getItem(int arg0) {
        // TODO Auto-generated method stub

        return this.alPicture.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = (RelativeLayout) RelativeLayout.inflate(context,
                    R.layout.item_list, null);

        }

        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
        TextView tv = (TextView) convertView.findViewById(R.id.textView1);

        iv.setImageDrawable(context.getResources().getDrawable(
                alPicture.get(position).getId()));
        tv.setText(alPicture.get(position).getName());
        // System.out.println("names of "+alPicture.get(position).getName());
        iv.setContentDescription(this.alPicture.get(position).getName());

        return convertView;
    }

    public void addPicture(OneItem onItem, int position) {

        if (position &lt; alPicture.size()) {
            // System.out.println("in add picture if condition");
            alPicture.add(position, onItem);
        } else {
            // System.out.println("in add picture else condition");
            alPicture.add(onItem);
        }

        notifyDataSetChanged();
    }

    public void addPicture(OneItem onItem) {

        alPicture.add(onItem);

        notifyDataSetChanged();
    }

    public void removeItem(int arg1) {
        alPicture.remove(arg1);
        notifyDataSetChanged();
    }

}
</code></pre>
