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?