ListView
のアイテムの位置を変更することができましたが、アプリケーションを終了した後、変更前の位置に戻ります。閉じるの後ListViewの位置が保存されない
アイテムの配置位置を保存するにはどうすればよいですか?アイテムの位置を変更し、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();
}
に保存する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);
}
を移植
編集
Array
内のアイテムがdataDr = getApplicationInfo().dataDir;
おそらく私のディレクトリに配置位置を保存する方法を見つける必要がありますか?
アイテムの位置が再び変更されたら、あなたは、アプリケーションを最小化し、再びやり直すときや完全にアプリケーションをシャットダウンするとき? –
@AwaisAhmadは私のアプリケーションを完全にシャットダウンします。今度は、新しいアクティビティを開いたり、デバイスの戻るボタンを押したときにオリジナルに戻ることに気付きました。 –
は、sharedpreference内の位置を保持し、アプリを起動するときに設定します。 – Rich