0

I created login page and register page. When I register with username and password then it is stored in database. When I go to login page, username and password was taken from the database. Everything ok till now.

After that I want to display the listview datas from database when login will successfully entered. Here is my login page:

package com.example.uidesign;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class signinactivity extends Activity {
    EditText edname,edpwd;
    Button b;
    LoginDataBaseAdapter logindatabaseadapter;
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        logindatabaseadapter=new LoginDataBaseAdapter(this);
        logindatabaseadapter=logindatabaseadapter.open();
        edname=(EditText) findViewById(R.id.editTextUserNameToLogin);
        edpwd=(EditText) findViewById(R.id.editTextPasswordToLogin);
        b=(Button) findViewById(R.id.buttonSignIn);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String userName=edname.getText().toString();
                String password=edpwd.getText().toString();
                String storedpassword=logindatabaseadapter.getSinlgeEntry(userName);
                if(password.equals(storedpassword))
                {
                    Toast.makeText(signinactivity.this, "Logged in Successfully", Toast.LENGTH_LONG).show();
                    Intent i=new Intent(getApplicationContext(),CategoryListActivity.class);
                    startActivity(i);
                }
                else
                {
                    Toast.makeText(signinactivity.this, "Password Does not match", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

}

when "Logged in successfully" message shown then i want to go to the listview database activity which will shown the data in a listview.

Here is my Listview class:

package com.example.uidesign;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class CategoryListActivity extends ListActivity {
    ListView lists;
    Button b;
    ListAdapter la;
    LoginDataBaseAdapter logindatabaseadapter;

    protected void oncreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categorieslist);
        lists=(ListView) findViewById(R.id.lv);
        logindatabaseadapter=new LoginDataBaseAdapter(this);
        logindatabaseadapter=logindatabaseadapter.open();
        List<String> values=logindatabaseadapter.getname();
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,values);
        setListAdapter(adapter);

    }

}

Here is my database adapter class:

public List<String> getname()
        {
            String [] column=new String[] {"ID","NAME"};
            Cursor c=db.query("category", column, null,null, null, null, null);
            List<String> li=new ArrayList<String>();
            int irow=c.getColumnIndex("ID");
            int iname=c.getColumnIndex("NAME");
            for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                li.add(c.getString(irow)+"  "+c.getString(iname));
            }
            return li;
        }

Here is dbhelper class.In this i have two table one for storing login information.other one for listview. Dbhelper class:

package com.example.uidesign;


import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper
{
    public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
    {
               super(context, name, factory, version);
    }
    // Called when no database exists in disk and the helper class needs
    // to create a new one.
    @Override
    public void onCreate(SQLiteDatabase _db) 
    {
            _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
            _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE1);

            ContentValues cv=new ContentValues();
            cv.put("NAME", "TAXI");
            _db.insert("category", null, cv);

            cv.put("NAME", "FOOD ORDERING");
            _db.insert("category", null, cv);

            cv.put("NAME", "HOSPITAL");
            _db.insert("category", null, cv);

            cv.put("NAME", "TICKET BOOKING");
            _db.insert("category", null, cv);

            cv.put("NAME", "POLICE");
            _db.insert("category", null, cv);

            cv.put("NAME", "EMERGENCY");
            _db.insert("category", null, cv);

    }
    // Called when there is a database version mismatch meaning that the version
    // of the database on disk needs to be upgraded to the current version.
    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
    {
            // Log the version upgrade.
            Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");


            // Upgrade the existing database to conform to the new version. Multiple
            // previous versions can be handled by comparing _oldVersion and _newVersion
            // values.
            // The simplest case is to drop the old table and create a new one.
            _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
            _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
            // Create a new one.
            onCreate(_db);
    }


}

and this is my overall adapter class. LoginDatabaseAdapter class:

package com.example.uidesign;

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;

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,PASSWORD text); ";
        static final String DATABASE_CREATE1="create table "+"category"+"(" +"ID" + " integer primary key autoincrement, " + "NAME 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 List<String> getname()
        {
            String [] column=new String[] {"ID","NAME"};
            Cursor c=db.query("category", column, null,null, null, null, null);
            List<String> li=new ArrayList<String>();
            int irow=c.getColumnIndex("ID");
            int iname=c.getColumnIndex("NAME");
            for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                li.add(c.getString(irow)+"  "+c.getString(iname));
            }
            return li;
        }

        public void insertEntry(String userName,String password)
        {
           ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", userName);
            newValues.put("PASSWORD",password);

            // 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("PASSWORD"));
            cursor.close();
            return password;                
        }
        public void  updateEntry(String userName,String password)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put("USERNAME", userName);
            updatedValues.put("PASSWORD",password);

            String where="USERNAME = ?";
            db.update("LOGIN",updatedValues, where, new String[]{userName});               
        }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2389035
  • 87
  • 1
  • 1
  • 8
  • are you getting any exception? or is that list empty? Could you give some more info – G_S Aug 12 '13 at 11:22
  • when logged in successfully then an empty screen appears – user2389035 Aug 12 '13 at 11:28
  • Check whether you are getting values into the variable 'value'. If you are not getting required data, check the cursor for the same. If you are not getting data even at cursor, Can you once check your database whether it has values or not? – G_S Aug 12 '13 at 11:39
  • I couldn't get it..anyother solution – user2389035 Aug 12 '13 at 12:01
  • what couldn't you get? Didnt you understand what i said? – G_S Aug 12 '13 at 12:04
  • exactly.please explain me briefly. – user2389035 Aug 12 '13 at 12:12
  • Hey check your database whether the database from which you are trying to get data has some rows or not – G_S Aug 12 '13 at 12:18
  • ok. But when i go to windows-> preferences-> open perspective, then open DDMS, and choose File Explorer and inside that data->data-> Here I saw the database name.But I couldn't open it.I don't know why? – user2389035 Aug 12 '13 at 12:23
  • http://stackoverflow.com/questions/6333412/how-to-view-sql-database-in-eclipse-debug-mode-for-android or http://www.basic4ppc.com/android/forum/threads/very-light-and-simple-to-use-sqlite-database-browser-editor-and-creation.23428/ check them. they may help you – G_S Aug 12 '13 at 12:29

0 Answers0