简体   繁体   English

如何在Android的“共享首选项”中使用主键?

[英]How to use primary key in Shared Preferences in android?

I am novice to android. 我是android的新手。 I want to use sharedpreferences in my app. 我想在我的应用程序中使用sharedpreferences。 I want to use id as primary key so that it is auto incremented whenever I add a new string value. 我想使用id作为主键,以便每当我添加新的字符串值时它都会自动递增。 It can be done by using SQLite, but that is very lengthy task. 可以通过使用SQLite来完成,但这是一个非常漫长的任务。 I want to make my app simple. 我想简化我的应用程序。

The insert.xml file is as follows: insert.xml文件如下:

package com.example.shiza.dailyquranquote;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class InsertVerse extends ActionBarActivity {
    EditText verseContent;
    EditText verseId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_verse);
    }
    public void saveVerse( View view)
    {
        verseContent = (EditText)findViewById(R.id.insertVerse);
        verseId = (EditText)findViewById(R.id.insertId);


        SharedPreferences sharedPreferences = getSharedPreferences("QuranVerse",0);

        Editor editor = sharedPreferences.edit();

        editor.putInt("id", Integer.parseInt( verseId.getText().toString()));
        editor.putString("verse", verseContent.getText().toString());

    }

}

SharedPreferences is not a database. SharedPreferences不是数据库。 It's a file with pair key/value stored. 这是一个存储有配对键/值的文件。

So for instance if you put a pair "id"/5, after you put a pair "id"/6, when the pair "id"/5 will be replaced by new pair "id"/6. 因此,例如,如果您放置了对“ id” / 5,则在放置了对“ id” / 6之后,当对“ id” / 5将被新的对“ id” / 6代替。

Please have a look at: 请看一下:

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

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

EDIT 1 编辑1

SharedPreferences is typically used to store settings, counters, parameters, info about owner of device,... SharedPreferences通常用于存储设置,计数器,参数,有关设备所有者的信息,...

Database are used to store multiple same records, for instance, list of users, list of products, ... 数据库用于存储多个相同的记录,例如,用户列表,产品列表,...

In SharedPreferences you can't set primary key because it's a Key/Value pair. SharedPreferences您不能设置主键,因为它是一个键/值对。 but, you can surely have multiple value for one key (separated by special character) like 但是,您肯定可以为一个键设置多个值(用特殊字符分隔),例如

id: 0;1;2

and

name: John Smith;Rickey Watson;Martin Guptill

Now, 现在,

0 is John Smith,
1 is Ricky watson and 
2 is Marting Guptill.

this is might be something you're looking for... 这可能是您要寻找的东西...

Applicable only if you want one record (key) and every time you add new String the id count will be incremented. 需要一条记录(键)且每次添加新的String时,id计数才会增加。 But if you have multiple records follow @LaurentY and @user94685231 但是,如果您有多个记录,请遵循@LaurentY和@ user94685231

public void saveVerse( View view)
        {
            verseContent = (EditText)findViewById(R.id.insertVerse);
            verseId = (EditText)findViewById(R.id.insertId);


            SharedPreferences sharedPreferences = getSharedPreferences("QuranVerse",0);
            int count=sharedPreferences.getInt("id",0);
            count++;
            Editor editor = sharedPreferences.edit();

            editor.putInt("id", count);
            editor.putString("verse", verseContent.getText().toString());

        }

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

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