gpt4 book ai didi

java - 如何在android的共享首选项中使用主键?

转载 作者:搜寻专家 更新时间:2023-11-01 08:44:55 26 4
gpt4 key购买 nike

我是安卓新手。我想在我的应用程序中使用共享首选项。我想使用 id 作为主键,这样每当我添加一个新的字符串值时它就会自动递增。它可以通过使用 SQLite 来完成,但这是一项非常漫长的任务。我想让我的应用程序变得简单。

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 不是数据库。这是一个存储有键/值对的文件。

例如,如果您放置一对“id”/5,在放置一对“id”/6 之后,“id”/5 将被新的“id”/6 对替换。

请看:

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

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

编辑 1

SharedPreferences 通常用于存储设置、计数器、参数、有关设备所有者的信息...

数据库用于存储多个相同的记录,例如,用户列表,产品列表,...

关于java - 如何在android的共享首选项中使用主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com