2016-09-02 9 views
0

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;

から充填されたことに注意してください。

おそらく私のディレクトリに配置位置を保存する方法を見つける必要がありますか?

+0

アイテムの位置が再び変更されたら、あなたは、アプリケーションを最小化し、再びやり直すときや完全にアプリケーションをシャットダウンするとき? –

+0

@AwaisAhmadは私のアプリケーションを完全にシャットダウンします。今度は、新しいアクティビティを開いたり、デバイスの戻るボタンを押したときにオリジナルに戻ることに気付きました。 –

+0

は、sharedpreference内の位置を保持し、アプリを起動するときに設定します。 – Rich

答えて

1

コメントですでに述べたように、SharedPreferencesはこのトリックを行います。 onCreateメソッドで初期化してください。

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

アイテムの位置が変更される可能性があるため、位置の代わりにファイル名を保存することをおすすめします。これの良い場所はonItemSelectedです(そのため、リストビューにリスナーを追加する必要があります)。

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(); 
    } 
}); 

あなたSavedFileオブジェクト内のファイルの位置を取得する必要がありますあなたのアプリを起動した後に正しい位置を設定します。 SharedPreferencesから

読み込んで文字列:あなたのリストビューとアダプタの初期化後

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

リストだけを反復処理し、文字列を比較します。文字列が一致する場合は、その位置を取得してlistviewオブジェクトに設定します。

for(int i = 0; i < SavedFiles.length; i++){ 
    if(fileName.equals(SavedFiles[i])){ 
    listview.setSelection(i); 
    break; 
    } 
} 
関連する問題