我想存儲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);
}
});
}
}
可以ArrayList的數據 只是改變ArrayList的數據使用GSON存入sharedprefrence以JSON和存儲到共享prefrence – Lovekesh
只需使用數據庫或sharedprefrence – Piyush
也您可以將arraylist存儲在文件 –