I am developing an android application in when user enters in the app, the first activity asks his name to enter in textfield. By clicking on submit button, the name of the user stores in SQLite database and another activity opens and the name of the user will display in next activity. Now, I want something like that, if user closes the app and when he starts it again, then the name of the user should not be asked again and he should directly move to second activity with his name that he entered first time. This is my complete code below :
DatabaseHandler.java
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="Database1";// Database Name...
private static final String TABLE_NAME = "Name"; // Name of the Table...
private static final String KEY_ID = "id"; // Column1 of the table
private static final String KEY_NAME="UserName";// Column2 of the table
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "(" + KEY_ID
+ " integer primary key autoincrement, " + KEY_NAME
+ " text not null);";
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHandler.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addName(Name name)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_NAME, name.getUsername());
db.insert(TABLE_NAME, null, values);
db.close();
}
public Name getName(int id)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(TABLE_NAME, new String[]{ KEY_ID,KEY_NAME },KEY_ID +"=?",new String[] { String.valueOf(id) },null,null,null,null);
if(cursor!=null)
{
cursor.moveToFirst();
}
Name name = new Name(Integer.parseInt(cursor.getString(0)),cursor.getString(1));
return name;
}
public List<Name> getAllNames() {
List<Name> nameList = new ArrayList<Name>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Name name = new Name();
name.setId(Integer.parseInt(cursor.getString(0)));
name.setUsername(cursor.getString(1));
// Adding contact to list
nameList.add(name);
} while (cursor.moveToNext());
}
return nameList;
}
}
MainActivity.java
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity
{
Button submit;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
final DatabaseHandler db = new DatabaseHandler(this);
submit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v1)
{
if(v1==submit)
{
String name1=et1.getText().toString();
Log.d("Insert: ", "Inserting ..");
db.addName(new Name(et1.getText().toString()));
Log.d("Reading: ", "Reading all contacts..");
List<Name> names = db.getAllNames();
for (Name n : names) {
String log = "Id: "+n.getId()+" ,Name: " + n.getUsername();
Log.d("Name: ", log);
}
Intent i1=new Intent(getApplicationContext(),NextPage.class);
i1.putExtra("name",name1);
startActivity(i1);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return super.onOptionsItemSelected(item);
}
}
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.database.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:hint="Please Enter Your Name"
android:inputType="textPersonName" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text=" Submit " />
</RelativeLayout>
I am not getting any idea about this how to do it.Please help me...