mardi 31 mars 2015

How to use primary key in Shared Preferences in android?

I am novice to android. I want to use sharedpreferences in my app. I want to use id as primary key so that it is auto incremented whenever I add a new string value. It can be done by using SQLite, but that is very lengthy task. I want to make my app simple.


The insert.xml file is as follows:



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());

}

}

Aucun commentaire:

Enregistrer un commentaire