1

ユーザーが追加ボタンをクリックするたびに特定のデータを保存するリストビューがあります。しかし、今まで私は実装にはほとんど成功していませんでした。 sharedpreferencesを使って行う必要があり、sharedpreferencesの保存状態とリストア状態を実装しようとしましたが、まだリストビューは現在クリックされている要素だけを追加します。アンドロイドにアクティビティを残した後、リストビューを保持するにはどうすればよいですか?

package com.example.daniel.pset3_daniel_jacob; 

import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 

public class listview extends AppCompatActivity { 

    ArrayList<String> dataa; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     onRestoredPreferences(); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_listview); 
     Bundle extras = getIntent().getExtras(); 
     String dataExtra = extras.getString("key"); 
     dataa = new ArrayList<String>(); 
     dataa.add(dataExtra); 
     ListView listView = (ListView) findViewById(R.id.listview); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa); 
     assert listView != null; 
     listView.setAdapter(adapter); 
     Savedpreferences(dataa); 


    } 

    public void Savedpreferences(List<String> dataa) { 


     SharedPreferences preferences = this.getSharedPreferences("object", 0); 
     SharedPreferences.Editor editor = preferences.edit(); 
     List<String> arraylist = new ArrayList<String>(dataa); 
     Set<String> newset = new HashSet<String>(arraylist); 
     editor.putStringSet("stringset", newset); 
     editor.commit(); 
    } 


    public void onRestoredPreferences() 
    { 
     SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0); 
     Set<String> getdataback = preferences.getStringSet("stringset", null); 
     List <String> arraylist = new ArrayList<String>(getdataback); 
    } 

} 

私は誰かが以下のコードを試してみて、すべての問題なら、私に知らせて、私が間違って

+0

あなたはそれがSharedPreferenceに保存したり、改行としてファイルに書き込むために良好なSので(後で使用するために保存したい場合) –

答えて

0

をやっているものを私に伝えるために私を助けることができると思います。

public class ListviewActivity extends AppCompatActivity { 

     ArrayList<String> dataa; 

     List <String> arraylist = new ArrayList<>(); 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      onRestoredPreferences(); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_listview); 
      Bundle extras = getIntent().getExtras(); 
      String dataExtra = extras.getString("key"); 
      dataa = new ArrayList<String>(); 
      dataa.addAll(arraylist); 
      dataa.add(dataExtra); 
      ListView listView = (ListView) findViewById(R.id.listview); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa); 
      assert listView != null; 
      listView.setAdapter(adapter); 
      Savedpreferences(dataa); 


     } 

     public void Savedpreferences(List<String> dataa) { 


      SharedPreferences preferences = this.getSharedPreferences("object", 0); 
      SharedPreferences.Editor editor = preferences.edit(); 
      List<String> arraylist = new ArrayList<String>(dataa); 
      Set<String> newset = new HashSet<String>(arraylist); 
      editor.putStringSet("stringset", newset); 
      editor.commit(); 
     } 


     public void onRestoredPreferences() 
     { 
      SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0); 
      Set<String> getdataback = preferences.getStringSet("stringset", null); 
      arraylist.clear(); 
      arraylist.addAll(getdataback); 
     } 

    } 
+0

あなたはその権利 'onRestoreInstanceState'と' onSaveInstanceState'は、まさにこの目的のために利用可能な方法が、知っています? –

+0

はい私は知っていますが、彼がそれを認識していない場合、答えを複雑にしたいと思っています。 – RaghavPai

+0

私は彼の問題を解決するために彼自身のロジックを使用しました – RaghavPai

関連する問題