Drag Drop and Reorder Items to ListView

I am following this library to add drag and drop functionality to my list. Everything is good when you move the first item to the second place. But when I move the first item of the list to the third position, the apps crash. To explain my situation, I have added an image. All I think is called on the Swap items line, but I think my code is correct. When I try to replace the first item with the third item, the application crashes. Any help would be appreciated for me. Thanks for the help.

enter image description here

Here is my adapter code.

public class NavigationDrawerListViewAdapter extends BaseAdapter implements Communicator,Swappable{

    private LayoutInflater mInflater;
    public ArrayList<NavigationDrawerFragment.ListItem> myItems;



    public NavigationDrawerListViewAdapter(Context activity, ArrayList<NavigationDrawerFragment.ListItem> listItemElements) {
        mInflater = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        myItems = listItemElements;

    }


    @Override
    public int getCount() {
        return myItems.size();
    }

    @Override
    public NavigationDrawerFragment.ListItem getItem(int position) {
        return myItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return myItems.get(position).hashCode();
    }

    @Override
    public boolean hasStableIds(){
        return true;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        final ViewHolder holder;
        final NavigationDrawerFragment.ListItem i = myItems.get(position);
        if (i != null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.navigation_drawer_listview_simple, null);
                holder.text = (TextView) convertView.findViewById(R.id.textView123);
                holder.text.setText(i.textdata);
                convertView.setTag(holder);
                parent.setTag(holder);
                holder.text.setTag(position);


        }
        return convertView;
    }

    @Override
    public void respond(String name) {

    }

    @Override
    public void sendData(String url, String type) {

    }

    @Override
    public void sendMapValues(Geometry featureGeometry, Symbol featureSymbol, Object featureAttrValues) {
            //doinNothing
    }

    @Override
    public void swapItems(int i, int i2) {

        NavigationDrawerFragment.ListItem obj1 = myItems.get(i);
        myItems.remove(i);
        myItems.add(i2,obj1);
        this.notifyDataSetChanged();

    }


    class ViewHolder {
        TextView text;
    }

}

      

0


source to share


1 answer


I think your code keeps adding the first element to the second position, but the second element is being removed. This happens inside the ArrayList, not in the GUI.

I propose a method that simply replaces two items, a basic replacement using a temporary object. This time temp is obj1. Sample code using your style:



public void swapItems(int i, int i2) {
   NavigationDrawerFragment.ListItem obj1 = myItems.get(i);
   myItems.set(i, myItems.get(i2);
   myItems.set(i2, obj1);
}

      

Note. I am using ArrayList method instead of add ().

+2


source







All Articles