简体   繁体   English

如何在Android应用程序中存储数据

[英]How to Store Data in Android Application

I have some codes here that user can set a value using EditText in my layout. 我在这里有一些代码,用户可以在我的布局中使用EditText设置一个值。 When user set number in text input and clicks on "set" button, value of EditText input, transmitted to a TextView . 当用户在文本输入中设置数字并单击“设置”按钮时,EditText输入的值,传输到TextView This is some image of that. 这是一些形象。 在此输入图像描述

Here is my XML: 这是我的XML:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textSize="25sp"
        android:id="@+id/textview" />
    <EditText
        android:id="@+id/edittext"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:inputType="number"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:id="@+id/set" />

And java codes: 和Java代码:

textview= (TextView)findViewById(R.id.textview);
edittext= (EditText) findViewById(R.id.edittext);
set = (Button) rootView.findViewById(R.id.set);

set.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        textview.setText(edittext.getText().toString());
    }
});

Now I want to keep value of "textview" after restarting app or device, it's matter to save all my data in internal storage. 现在,我想在重新启动应用程序或设备后保留“ textview”的值,将所有数据保存在内部存储中很重要。

You can use SharedPreferences to store simple data like that. 您可以使用SharedPreferences这样存储简单数据。 If you need to save bigger amounts, use a database. 如果需要节省更多金额,请使用数据库。

first Sqlite databse 第一个Sqlite数据库

Senad SharedPreferences Senad共享首选项

Third internet storage(SdCarc) 第三网络存储(SdCarc)

使用SharedPreferences并获取进一步的帮助来看看这个

Check this link if you need to know more about SharedPreferences Helper Class 如果您需要了解有关SharedPreferences Helper Class的更多信息,请查看此链接

http://developer.android.com/guide/topics/data/data-storage.html#pref http://developer.android.com/guide/topics/data/data-storage.html#pref

For saving of that kind of the information for you is quite nice to use SharedPreferences . 为了保存这类信息,使用SharedPreferences非常好。 This is quite easy and fast way to manage data. 这是一种非常简单快捷的数据管理方式。

To write : 来写 :

SharedPreferences preferences = getSharedPreferences("prefData", Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("fristRow",userid.getText().toString());
editor.putString("secondRow",password.getText().toString());
editor.apply();

To Read : 读书 :

SharedPreferences preferences = getSharedPreferences("prefData", Context.MODE_PRIVATE);
String Astatus = preferences.getString("fristRow", "");

You can use SharedPreferences for this purpose 您可以为此目的使用SharedPreferences

set.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        textview.setText(edittext.getText().toString());
                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                        Editor prefsEditor = prefs.edit();
                        prefsEditor.putString("text_value", edittext.getText().toString());
                        prefsEditor.commit();
                    }
                });

Inorder to read the value 为了读取值

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String textValue = prefs.getString("text_value", "");

To store some simple data, you can use SharedPreferences 要存储一些简单数据,可以使用SharedPreferences

  SharedPreferences sp = getSharedPreferences(PRE_NAME, 0);//get the preference object
  SharedPreferences.Editor editor = sp.edit();
  editor.putString("key_here", value_here);//store some value(key-->value)
  editor.commit();//commit the changes

and you can use sp.getXXXX() to get the data stored: 并且您可以使用sp.getXXXX()来获取存储的数据:

   SharedPreferences sp= getSharedPreferences(PREFS_NAME, 0);
   String storedValue = sp.getString("your_key", default_value);

If you want some data stored in structure, you can use SQL Lite. 如果要将某些数据存储在结构中,则可以使用SQL Lite。

Please refer to Storage Options for more details. 请参阅存储选项以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何存储电子商务android应用程序的产品列表 - how to store product list for an ecommerce android application 如何初始化Application类并在android中存储getApplicationContext()? - How to initialize an Application class and store getApplicationContext() in android? 如何使用Java在Android中存储和读取我的应用程序的用户使用情况数据? - How to store and read user usage data of my application in Android using java? 如何在用户的谷歌驱动器中存储 android kotlin 应用程序的应用程序特定数据? - How to Store application-specific data of the android kotlin app in user's google drive? 如何在JavaFX应用程序中存储数据 - How to store data within javafx application 如何在Java应用程序中存储数据? - How to store the data within java application? 桌面应用程序-如何存储数据以进行多次启动 - Desktop application - How to store Data for multiple boots 存储应用程序初始化数据的方式和位置? - How and where to store application initialization data? 如何为独立的Java应用程序存储简单数据 - How to store simple data for standalone java application 为 android 应用程序存储 static 数据的最佳方式是什么? - What is the best way to store static data for an android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM