<NME> DragNDropSimpleAdapter.java
<BEF> /*
 * Copyright 2012 Terlici Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.terlici.dragndroplist;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class DragNDropSimpleAdapter extends SimpleAdapter implements DragNDropAdapter {
	int mPosition[];
	int mHandler;
	
	public DragNDropSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, int handler) {
		super(context, data, resource, from, to);
		
		mHandler = handler;
		setup(data.size());
	}

	private void setup(int size) {
		mPosition = new int[size];
		
		for (int i = 0; i < size; ++i) mPosition[i] = i;
	}
	
	@Override
	public View getDropDownView(int position, View view, ViewGroup group) {
		return super.getDropDownView(mPosition[position], view, group);
	}
	
	@Override
	public Object getItem(int position) {
		return super.getItem(mPosition[position]);
	}
	
	@Override
	public int getItemViewType(int position) {
		return super.getItemViewType(mPosition[position]);
	}
	
	@Override
	public long getItemId(int position) {
		return super.getItemId(mPosition[position]);
	}
	
	@Override
	public View getView(int position, View view, ViewGroup group) {
		return super.getView(mPosition[position], view, group);
	}
	
	@Override
	public boolean isEnabled(int position) {
		return super.isEnabled(mPosition[position]);
	}

	@Override
	public void onItemDrag(DragNDropListView parent, View view, int position, long id) {
		
	}

	@Override
	public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id) {
		int position = mPosition[startPosition];
		
		if (startPosition < endPosition)
			for(int i = startPosition; i < endPosition; ++i)
				mPosition[i] = mPosition[i + 1];
		else if (endPosition < startPosition)
			for(int i = startPosition; i > endPosition; --i)
				mPosition[i] = mPosition[i - 1];
		
		mPosition[endPosition] = position;
	}

	@Override
	public int getDragHandler() {
		return mHandler;
	}
}

<MSG> -Also Fixed issue related to DragNDropSimpleadapter background data list change after initialization. (Made notifyDataSetChanged change background data set)
- Updated Support Library Version to 21.0.3
- Updated Android Target Version to 5.0.1

<DFF> @@ -25,47 +25,51 @@ import android.view.ViewGroup;
 import android.widget.SimpleAdapter;
 
 public class DragNDropSimpleAdapter extends SimpleAdapter implements DragNDropAdapter {
-	int mPosition[];
-	int mHandler;
-	
-	public DragNDropSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, int handler) {
+	private int mPosition[];
+	private int mHandler;
+	private List<? extends Map<String, ?>> dataList;
+
+	public DragNDropSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,
+			int[] to, int handler) {
 		super(context, data, resource, from, to);
-		
+
 		mHandler = handler;
+		dataList = data;
 		setup(data.size());
 	}
 
 	private void setup(int size) {
 		mPosition = new int[size];
-		
-		for (int i = 0; i < size; ++i) mPosition[i] = i;
+
+		for (int i = 0; i < size; ++i)
+			mPosition[i] = i;
 	}
-	
+
 	@Override
 	public View getDropDownView(int position, View view, ViewGroup group) {
 		return super.getDropDownView(mPosition[position], view, group);
 	}
-	
+
 	@Override
 	public Object getItem(int position) {
 		return super.getItem(mPosition[position]);
 	}
-	
+
 	@Override
 	public int getItemViewType(int position) {
 		return super.getItemViewType(mPosition[position]);
 	}
-	
+
 	@Override
 	public long getItemId(int position) {
 		return super.getItemId(mPosition[position]);
 	}
-	
+
 	@Override
 	public View getView(int position, View view, ViewGroup group) {
 		return super.getView(mPosition[position], view, group);
 	}
-	
+
 	@Override
 	public boolean isEnabled(int position) {
 		return super.isEnabled(mPosition[position]);
@@ -73,23 +77,34 @@ public class DragNDropSimpleAdapter extends SimpleAdapter implements DragNDropAd
 
 	@Override
 	public void onItemDrag(DragNDropListView parent, View view, int position, long id) {
-		
+
 	}
 
 	@Override
 	public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id) {
 		int position = mPosition[startPosition];
-		
+
 		if (startPosition < endPosition)
-			for(int i = startPosition; i < endPosition; ++i)
+			for (int i = startPosition; i < endPosition; ++i)
 				mPosition[i] = mPosition[i + 1];
 		else if (endPosition < startPosition)
-			for(int i = startPosition; i > endPosition; --i)
+			for (int i = startPosition; i > endPosition; --i)
 				mPosition[i] = mPosition[i - 1];
-		
+
 		mPosition[endPosition] = position;
 	}
 
+	@Override
+	public int getCount() {
+		return dataList.size();
+	}
+
+	@Override
+	public void notifyDataSetChanged() {
+		setup(dataList.size());
+		super.notifyDataSetChanged();
+	}
+
 	@Override
 	public int getDragHandler() {
 		return mHandler;

{'addition_count': 33, 'commit_subject': '-Also Fixed issue related to DragNDropSimpleadapter background data list change after initialization. (Made notifyDataSetChanged change background data set) - Updated Support Library Version to 21.0.3 - Updated Android Target Version to 5.0.1', 'deletion_count': 18, 'file_extension': '.java', 'lang': 'java', 'license': 'apache-2.0', 'repo_name': 'terlici/DragNDropList'}