0

I managed to change my ListView's item position but after I close my app, the position goes back to before I made any changes.

What and how do I save the item's arranged position?

Populating ListView

String[] SavedFiles;
String dataDr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address);

    dataDr = getApplicationInfo().dataDir;
    showDirFile(dataDr);
}

void showDirFile(String dirpth)
{
    String path = dirpth+"/files";
    Log.d("Files", "Path: " + path);
    File f = new File(path);
    File file[] = f.listFiles();
    Log.d("Files", "Size: "+ file.length);

    SavedFiles = new String[file.length];
    for (int i=0; i < file.length; i++)
    {
        Log.d("Files", "FileName:" + file[i].getName());

        SavedFiles[i] = file[i].getName();
    }

    ArrayAdapter<String> adapter
            = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            SavedFiles);
    listView.setAdapter(adapter);
}

to change item position and save them to an Array

Collections collections;

void positionChange(){

    //to store arrays into ArrayList
    List<String> newList = new ArrayList<String>(Arrays.asList(myDataFiles));

    //to get the item's position @ get the first item in array if multiple arrays exist
    String currentPos = String.valueOf(intArrayList.get(0));
    int oldPos = Integer.valueOf(currentPos);
    int newPos = oldPos-1;

    //Swap position @ move up list
    collections.swap(newList, oldPos, newPos);

    //store ArrayList data into arrays
    myDataFiles = newList.toArray(myDataFiles);

    intArrayList.clear();

    adapter.notifyDataSetChanged();
}

Edit

Do note that the items in the Array was filled from dataDr = getApplicationInfo().dataDir;

Perhaps I need to find a way to save the arrangement position in my directory?

Fay Zan
  • 165
  • 2
  • 17
  • When your items changes their position again? When you minimize the app and resume again or when you completely shutdown the app? – Awais Ahmad Sep 02 '16 at 04:23
  • @AwaisAhmad when I completely shutdown my app. Now I noticed that it also reverts back to original when I open a new activity or press my device's back button – Fay Zan Sep 02 '16 at 04:39
  • just keep the position in a sharedpreference and set it on starting up the app – Rich Sep 02 '16 at 05:00
  • @Rithe alright, I've studied a bit about sharedpreference and it seems to do the job. But before I implement this, how will this retrieve my item's position since I fill my `Array` using `dataDr = getApplicationInfo().dataDir;`. Please post as answer because I'm not sure how to implement this – Fay Zan Sep 02 '16 at 06:36

1 Answers1

1

As I already mentioned in the comments, SharedPreferences will do the trick. Initialize it in your onCreate method.

// use member variable for this (private SharedPreferences prefs)
prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

Since the position of your item may change, it would be wise to store the file name instead of the postion. Good place for this could be the onItemSelected (therefor you have to add a listener to your listview).

listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    prefs.edit().putString("yourKey", listView.getItemAtPosition(position)).apply();
  }
});

To set the correct position after starting up your app you have to get the position of the file in your SavedFile object.

Loading string from SharedPreferences:

String fileName = prefs.getString("yourKey", ""); 

After initialization of your listview and adapter just iterate through the list and compare strings. If the string matches, take the position and set it to your listview object.

for(int i = 0; i < SavedFiles.length; i++){
  if(fileName.equals(SavedFiles[i])){
    listview.setSelection(i);
    break;
  }
}
Community
  • 1
  • 1
Rich
  • 1,015
  • 1
  • 10
  • 22