I would like show the data sqlite in the textview but i don't no how i do ?
LoginDataBaseAdapter.java
package com.example.audevardlast;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "CREATE TABLE " + "LOGIN" + " ("
+ "ID" + " INTEGER PRIMARY KEY AUTOINCREMENT, " + "USERNAME" + " TEXT, "
+"PRENOM" + " TEXT, "
+"EMAIL" + " TEXT, "
+"SCORE" + " TEXT, "
+ "CODE" + " TEXT );";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String prenom,String mail,String cp,String point)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PRENOM",prenom);
newValues.put("EMAIL",mail);
newValues.put("CODE",cp);
newValues.put("SCORE",point);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("USERNAME"));
cursor.close();
return password;
}
public void updateEntry(String userName,String prenom,String mail, String cp,String point)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PRENOM",prenom);
updatedValues.put("EMAIL",mail);
updatedValues.put("CODE",cp);
updatedValues.put("SCORE",point);
String where="EMAIL = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
SignUPActivity.java
package com.example.audevardlast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignUPActivity extends Activity
{
EditText editTextUserName,editTextPrenom,editTextEmail,editTextCodePostal,editTextPoint;
Button btnCreateAccount;
final String SCORE="score";
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
Intent intent = getIntent();
EditText loginDisplay = (EditText) findViewById(R.id.editText2);
if (intent != null)
{
loginDisplay.setText(intent.getStringExtra(SCORE));
}
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPrenom=(EditText)findViewById(R.id.editTextPassword);
editTextEmail=(EditText)findViewById(R.id.editTextMail);
editTextCodePostal=(EditText)findViewById(R.id.editText1);
editTextPoint=(EditText)findViewById(R.id.editText2);
editTextPoint.setVisibility(View.INVISIBLE);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String prenom=editTextPrenom.getText().toString();
String mail=editTextEmail.getText().toString();
String cp=editTextCodePostal.getText().toString();
String point=editTextPoint.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||prenom.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, prenom, mail, cp, point);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
}
I would like total data of my sqlite appear in a textview in a homeActivity...
HomeActivity.java
package com.example.audevardlast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class HomeActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
final String SCORE="score";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
TextView loginDisplay = (TextView) findViewById(R.id.textView1);
if (intent != null)
{
loginDisplay.setText(intent.getStringExtra(SCORE));
}
// create a instance of SQLite Database
loginDataBaseAdapter =new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get The Refference Of Buttons
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity and Start The Activity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
TextView score1=(TextView) findViewById(R.id.textView1);
intentSignUP.putExtra(SCORE, score1.getText().toString());
startActivity(intentSignUP);
}
});
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
{
final Dialog dialog = new Dialog(HomeActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
// get the Refferences of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(userName.equals(storedPassword))
{
Intent intent = new Intent(HomeActivity.this, ProfilActivity.class);
TextView score1=(TextView) findViewById(R.id.textView1);
intent.putExtra(SCORE, score1.getText().toString());
startActivity(intent);
}
else
{
Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
you can declare adapter like that
}
and after that write your activity code something like that
and make your xml files something like that
and declare listview like this
hope this helps ....
You can get all entries by returning a cursor inside Your LoginDatavaseAdapter class:
Then it´s easy in Your Activity:
Here what You have to use to get the information:
"ID" = cursor.getString(0)//or cursor.getInt(0) because it is an integer "USERNAME" = cursor.getString(1) "PRENOM" = cursor.getString(2) "EMAIL" = cursor.getString(3) "SCORE" = cursor.getString(4) "CODE" = cursor.getString(5)
This is how You get all informations from one row. In the for loop above, when You set
Then the cursor skip to the next row and gets the data from the row. If You just want to have only entries from one User, then it could be something like this: