简体   繁体   English

从 textview 保存数据

[英]saving data from textview

I created a DB in my application.我在我的应用程序中创建了一个数据库。 I have one TextView in my application, I need to save the data whatever in textview.我的应用程序中有一个 TextView,我需要将数据保存在 textview 中。 How can I do that?我怎样才能做到这一点?

In your database adapter class create a method to insert a single value in your database like在您的数据库适配器 class 中,创建一个在数据库中插入单个值的方法,例如

public void insert(String data)
    {
        ContentValues in=new ContentValues();
        in.put("data", data);

        db.insert(table_name, null, in);
    }

Create a object of your adapter class and then call the method insert with the value of your edit text.创建适配器 class 的 object,然后使用编辑文本的值调用方法 insert。

public void insert(Object data)
    {
        ContentValues in=new ContentValues();
        //in.put("data", (String) data);
        in.putAll((ContentValues) data);

        db.insert(table_name, null, in);
    } 

Here you can can pass any object to insert in the database.在这里您可以通过任何 object 插入数据库。

get data from textview:从 textview 获取数据:

String var = yourtextview.getText().toString();

save this variable as per your need..根据您的需要保存此变量..

Shared Preferences will be helpful to you to store data.共享首选项将有助于您存储数据。 Here is the docs.这是文档。

http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/reference/android/content/SharedPreferences.html

And the text from textview can be obtained using并且可以使用 textview 获得文本

String textToStore = textview.getText().toString;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM