Reputation: 725
I'm following this library to add drag - drop functionality my listview. Everything is fine when moving first item to second place. But when I move the listview first element to third place app crashes. In order to explain my situation I've added image. All I think it is caused at Swap items line but i think my code right. When I try to replace first item to third item, app is crashing. Any help will be valuable for me. Thanks for helpings.
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;
}
}
Upvotes: 0
Views: 753
Reputation: 6215
I think your code keeps adding the first item to the second position, but the second item gets removed. This happens internally in the ArrayList, not in the GUI.
I suggest a technique that just swaps the two items, basic swapping using a temporary object. This time the 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 the ArrayList set method instead of add().
Upvotes: 2