2016-12-28 17 views
1

我想存儲ArrayList中的ArrayThat並保存它,即使應用程序已關閉並再次打開。我想找回每當應用程序打開的值並顯示結果我想將ArrayList存儲到離線存儲中,每次點擊接受按鈕並在應用程序重新打開時檢索它

This is my MainActivity 

package com.example.vineeth.customlistviewmytrail; 

    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.ListView; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class MainActivity extends AppCompatActivity { 


     int test; 
     EditText et_period1,et_period2,et_period3,et_period4,et_period5,et_period6,et_period7,et_period8; 
     EditText et_number; 
     TextView tv_head; 

     Button bt_submit,bt_submit2; 


     // 






     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      et_number = (EditText) findViewById(R.id.et_number); 
      bt_submit = (Button)findViewById(R.id.bt_submit); 
      bt_submit2=(Button)findViewById(R.id.bt_submit2); 
      tv_head = (TextView)findViewById(R.id.tv_head); 



      et_period1 = (EditText) findViewById(R.id.et_period1); 
      et_period2 = (EditText) findViewById(R.id.et_period2); 
      et_period3 = (EditText) findViewById(R.id.et_period3); 
      et_period4 = (EditText) findViewById(R.id.et_period4); 
      et_period5 = (EditText) findViewById(R.id.et_period5); 
      et_period6 = (EditText) findViewById(R.id.et_period6); 
      et_period7 = (EditText) findViewById(R.id.et_period7); 
      et_period8 = (EditText) findViewById(R.id.et_period8); 


      SharedPreferences.Editor editor = getSharedPreferences("text",MODE_PRIVATE).edit(); 
      editor.commit(); 
      SharedPreferences pref = getSharedPreferences("text",MODE_PRIVATE); 

      int bouncer = pref.getInt("bounce",0); 
      if(bouncer==0) 
      { 
       tv_head.setText("Enter for Monday"); 

      } 
      else if(bouncer==1) 
      { 
       tv_head.setText("Enter for Tuesday"); 

      } 
      else if(bouncer==2) 
      { 
       tv_head.setText("Enter for Wednesday"); 

      } 
      else if(bouncer ==3) 
      { 
       tv_head.setText("Enter for Thursday"); 
      } 
      else if(bouncer==4) 
      { 
       tv_head.setText("Enter for Friday"); 
      } 
      else if(bouncer==5) 
      { 
       tv_head.setText("Enter for Saturday"); 
      } 
      else{ 
       Toast.makeText(this, "Thank you for Entering all your values", Toast.LENGTH_SHORT).show(); 
      } 


      bt_submit.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        String value = et_number.getText().toString(); 
        test = 1; 


        if(value.isEmpty()) 
        { 
         Toast.makeText(MainActivity.this, "Enter something", Toast.LENGTH_SHORT).show(); 
        } 
        else { 

         int number = Integer.parseInt(value); 


         if (number < 2) { 
          Toast.makeText(MainActivity.this, "Enter valid value", Toast.LENGTH_SHORT).show(); 
         } 

         if (number == 2) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.INVISIBLE); 
          et_period4.setVisibility(View.INVISIBLE); 
          et_period5.setVisibility(View.INVISIBLE); 
          et_period6.setVisibility(View.INVISIBLE); 
          et_period7.setVisibility(View.INVISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 3) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.INVISIBLE); 
          et_period5.setVisibility(View.INVISIBLE); 
          et_period6.setVisibility(View.INVISIBLE); 
          et_period7.setVisibility(View.INVISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 4) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.VISIBLE); 
          et_period5.setVisibility(View.INVISIBLE); 
          et_period6.setVisibility(View.INVISIBLE); 
          et_period7.setVisibility(View.INVISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 5) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.VISIBLE); 
          et_period5.setVisibility(View.VISIBLE); 
          et_period6.setVisibility(View.INVISIBLE); 
          et_period7.setVisibility(View.INVISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 6) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.VISIBLE); 
          et_period5.setVisibility(View.VISIBLE); 
          et_period6.setVisibility(View.VISIBLE); 
          et_period7.setVisibility(View.INVISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 7) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.VISIBLE); 
          et_period5.setVisibility(View.VISIBLE); 
          et_period6.setVisibility(View.VISIBLE); 
          et_period7.setVisibility(View.VISIBLE); 
          et_period8.setVisibility(View.INVISIBLE); 
         } 
         if (number == 8) { 
          et_period1.setVisibility(View.VISIBLE); 
          et_period2.setVisibility(View.VISIBLE); 
          et_period3.setVisibility(View.VISIBLE); 
          et_period4.setVisibility(View.VISIBLE); 
          et_period5.setVisibility(View.VISIBLE); 
          et_period6.setVisibility(View.VISIBLE); 
          et_period7.setVisibility(View.VISIBLE); 
          et_period8.setVisibility(View.VISIBLE); 
         } 
        } 

       } 
      }); 


      bt_submit2.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        if(test==1) 
        { 
         String period1,period2,period3,period4,period5,period6,period7,period8; 

         period1 = et_period1.getText().toString(); 
         period2 = et_period2.getText().toString(); 
         period3 = et_period3.getText().toString(); 
         period4 = et_period4.getText().toString(); 
         period5 = et_period5.getText().toString(); 
         period6 = et_period6.getText().toString(); 
         period7 = et_period7.getText().toString(); 
         period8 = et_period8.getText().toString(); 


         Intent i = new Intent(MainActivity.this,Main2Activity.class); 
         i.putExtra("period1",period1); 
         i.putExtra("period2",period2); 
         i.putExtra("period3",period3); 
         i.putExtra("period4",period4); 
         i.putExtra("period5",period5); 
         i.putExtra("period6",period6); 
         i.putExtra("period7",period7); 
         i.putExtra("period8",period8); 

         startActivity(i); 

        } 

        else 
        { 
         Toast.makeText(MainActivity.this, "Enter values first", Toast.LENGTH_SHORT).show(); 
        } 







       } 
      }); 









     } 
    } 

我SecondActivity

This is my secondactivity 

package com.example.vineeth.customlistviewmytrail; 

import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.ArrayList; 

public class Main2Activity extends AppCompatActivity { 
    ListView lv_list; 
    Button bt_confirm,bt_clear; 
    ArrayList<String> list = new ArrayList<String>(); 
int bouncer; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

    lv_list = (ListView)findViewById(R.id.lv_list); 
    bt_confirm = (Button)findViewById(R.id.bt_confirm); 
    bt_clear = (Button)findViewById(R.id.bt_clear); 



    final Intent i = getIntent(); 
    final String value1 = i.getStringExtra("period1"); 
    list.add(value1); 
    String value2 = i.getStringExtra("period2"); 
    list.add(value2); 
    String value3 = i.getStringExtra("period3"); 
    list.add(value3); 
    String value4,value5,value6,value7,value8; 
    value4=i.getStringExtra("period4"); 
    list.add(value4); 
    value5=i.getStringExtra("period5"); 
    list.add(value5); 
    value6=i.getStringExtra("period6"); 
    list.add(value6); 
    value7=i.getStringExtra("period7"); 
    list.add(value7); 
    value8=i.getStringExtra("period8"); 
    list.add(value8); 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); 

    /*JSONObject json = new JSONObject(); 
    json.put("arrays",new JSONArray(list)); 
    String arrayList = json.toString();*/ 



    lv_list.setAdapter(adapter); 


    bt_confirm.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      SharedPreferences.Editor editor = getSharedPreferences("text",MODE_PRIVATE).edit(); 
      SharedPreferences pref = getSharedPreferences("text",MODE_PRIVATE); 





      bouncer = pref.getInt("bounce",0); 


      bouncer = bouncer + 1; 


      Intent intent = new Intent(Main2Activity.this,MainActivity.class); 


      editor.putInt("bounce",bouncer); 
      editor.commit(); 

      // Toast.makeText(Main2Activity.this, bouncer, Toast.LENGTH_SHORT).show(); 


      startActivity(intent); 

     } 
    }); 

    bt_clear.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      SharedPreferences.Editor editor = getSharedPreferences("text",MODE_PRIVATE).edit(); 
      bouncer=0; 
      editor.clear(); 
      editor.commit(); 

      Intent intent = new Intent(Main2Activity.this,MainActivity.class); 
      startActivity(intent); 



     } 
    }); 









} 

}

+0

可以ArrayList的數據 只是改變ArrayList的數據使用GSON存入sharedprefrence以JSON和存儲到共享prefrence – Lovekesh

+0

只需使用數據庫或sharedprefrence – Piyush

+1

也您可以將arraylist存儲在文件 –

回答

1

我得到了一個相當簡單的解決方案,以我的問題。

我使用了2個函數將數組轉換爲用逗號分隔的字符串格式,另一個函數使用split將字符串轉換回數組。

我將字符串很容易地以字符串格式存儲在SharedPreferences中,並在需要時將它們恢復並將其轉換回來。

轉換數組串碼

public String convertArrayToString(String[] array) 
    { 
     String str = ""; 
     for(int i=0;i<array.length;i++) 
     { 
      str = str+array[i]; 

      if(i<array.length-1) 
      { 
       str = str+strSeperator; 
      } 
     } 
     return str; 
    } 

轉換字符串到陣列碼

public static String[] convertStringToArray(String str) 
    { 
     String[] arr = str.split(strSeperator); 
     return arr; 
    } 

,然後簡單地 轉換該ArrayList正常陣列 這樣

String[] toArray = list.toArray(new String[list.size()]); 

,然後

我用了convertArrayTostring功能,並直接存儲在sharedpref文件

editor.putString("MondayList",convertArrayToString(toArray)); 
0

您可以將其存儲在數據庫或共享偏好。

  1. 定義你的模型,你可以使用一些數據存儲在SharedPreferences

  2. 添加GSON圖書館

  3. 獲取SharedPreferences

    更多有關SharedPreferences

    final String key = "example"; // constant key to retrieve data, must be unique 
    final String prefName = "example2" // constant name for preference file 
    final SharedPreferences pref = context.getSharedPreferences(prefName, Context.MODE_PRIVATE) 
    
    final Gson gson = new Gson(); 
    
    // put data 
    pref.edit().putString(key, gson.toJson(model)).apply(); 
    
    // get data 
    final String serialized = pref.getString(key, null); // if there is no data in preferences yet, it returns null 
    if (serialized == null){ 
        // handle no saved data state 
    } else { 
        final Model model = gson.fromJson(serialized, Model.class); 
    } 
    

就是這樣。您可以爲每個操作製作單獨的方法,將其包裝到界面中並在某處執行,或者製作簡單的PreferenceStorage/Helper class以方便訪問並保持清潔的架構。

+0

謝謝!我一定會嘗試! –

0

請嘗試以下代碼:
對於保存:

String mySavedList= new Gson().toJson(list); 
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("My_SAVED_LIST", mySavedList).commit(); 

檢索:

ArrayList<String> myList = new Gson().fromJson(PreferenceManager.getDefaultSharedPreferences(this).getString("My_SAVED_LIST", ""), new TypeToken<ArrayList<String>>() { 
       }.getType()); 
相關問題