Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:08:49+00:00 2026-05-29T06:08:49+00:00

I have a problem saving the list after edit the listview with drag and

  • 0

I have a problem saving the list after edit the listview with drag and drop.

I’am using the sourche code from here:Android Drag and Drop List

The code works fine but the new list order is not save when you exit and open the app again:

The first the listview is like this

a 
b 
c 

after drag and drop

c
b
a

but if i quit this app and then start it later , it will still be -> a b c

public class DragNDropListActivity extends ListActivity {

public static String[] mNewPositions; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.dragndroplistview);

    ArrayList<String> content = new ArrayList<String>(mListContent.length);
    for (int i=0; i < mListContent.length; i++) {
        content.add(mListContent[i]);
    }

    setListAdapter(new DragNDropAdapter(this, new int[]{R.layout.dragitem}, new int[]{R.id.TextView01}, content));//new DragNDropAdapter(this,content)
    ListView listView = getListView();

    if (listView instanceof DragNDropListView) {
        ((DragNDropListView) listView).setDropListener(mDropListener);
        ((DragNDropListView) listView).setRemoveListener(mRemoveListener);
        ((DragNDropListView) listView).setDragListener(mDragListener);
        ((DragNDropListView) listView).setPositionListener(mPositionListener);
    }
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String selection = (String) getListAdapter().getItem(position);
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     SharedPreferences.Editor editor = preferences.edit();
     editor.putString("selection", selection);
     editor.commit();
         Intent i = new Intent(this, DkNewsActivity.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         this.startActivity(i);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
           case (R.id.Info):
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse("market://search?q=pub:notToSee"));
              startActivity(intent);

                 break;
           case (R.id.Rate):

           SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("rateDone", 1);
            editor.commit();

               intent = new Intent(Intent.ACTION_VIEW);
              intent.setData(Uri.parse("market://details?id=notToSee"));
              startActivity(intent);
                 break;
       }
       return true;
   }
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    //set menu rate visible
    if (preferences.getInt("rateDone", 0) == 0){
        menu.getItem(1).setVisible(true);
    }
    else {
        menu.getItem(1).setVisible(false);
    }

    return true;
}

private PositionListener mPositionListener=new PositionListener(){
     public void tryToScrollInAndroid_1point5(int position) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            getListView().setSelection(position);  //android 1.5
        }
     }
};
private DropListener mDropListener = 
    new DropListener() {
    public void onDrop(int from, int to) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onDrop(from, to);
            getListView().invalidateViews();

            //Saving dragNDropList
            mNewPositions = new String[adapter.getCount()]; //Initialize your new items storage

            for(int i=0; i < adapter.getCount(); i++) {
                //Implement here your logic for save positions
                mNewPositions[i] = adapter.getItem(i).toString();
            }               
        }
    }
};

private RemoveListener mRemoveListener =
    new RemoveListener() {
    public void onRemove(int which) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onRemove(which);
            getListView().invalidateViews();
        }
    }
};

private DragListener mDragListener =
    new DragListener() {

    int backgroundColor = 0xe0103010;
    int defaultBackgroundColor;

        public void onDrag(int x, int y, ListView listView) {}

        public void onStartDrag(View itemView) {
            if (itemView != null){itemView.setVisibility(View.INVISIBLE);
            defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
            itemView.setBackgroundColor(backgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.INVISIBLE);
            }
        }

        public void onStopDrag(View itemView) {
            if (itemView != null){itemView.setVisibility(View.VISIBLE);
            itemView.setBackgroundColor(defaultBackgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.VISIBLE);}
        }

};

  private static String[] mListContent={
     "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7","Item 8", "Item 9", "Item 10"
    ,"Item 11", "Item 12", "Item 13", "Item 14", "Item 15", "Item 16", "Item 17","Item 18", "Item 19", "Item 20"};
}

I believe I have to do something under “private DropListener mDropListener” to save the change and the I need to read the new item position onCreate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:08:49+00:00Added an answer on May 29, 2026 at 6:08 am

    Why not put the String[] into SharedPreferences using the Editor in the onPause() method of the Activity (make one if its not there) and then get it in the onCreate() method. This way you will always have the most recent version. Right now you set it to mListContent every time which is static and doesn’t change. Have that be the default one, and instead get your last one from shared preferences. I assume your new order is in mNewPositions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CakePHP framework with MySQL database and I have problem in saving
We have a weird intermittent problem with saving from Word 2007 to our SharePoint
We have a problem with the Euro character when saving and retrieving it from
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
Okay, I'm having a problem saving after I've deleted all the objects I have
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
My answer: After getting annoyed, I have found a solution. The problem was indeed
Example: I have a list of friends which I retrieve as XML from a
I have some nice, working edit-undo functionality in my winforms application. It works using
I have problem in some JavaScript that I am writing where the Switch statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.