0

我创建了登录页面和注册页面。当我使用用户名和密码注册时,它会存储在数据库中。当我进入登录页面时,用户名和密码是从数据库中获取的。到目前为止一切正常。

之后,我想在登录成功时显示数据库中的列表视图数据。这是我的登录页面:

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

            }
        });
    }

}

当显示“登录成功”消息时,我想转到列表视图数据库活动,该活动将在列表视图中显示数据。

这是我的 Listview 课程:

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

    }

}

这是我的数据库适配器类:

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;
        }

这是 dbhelper 类。在此我有两个表,一个用于存储登录信息。另一个用于列表视图。Dbhelper 类:

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


}

这是我的整体适配器类。登录数据库适配器类:

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

}
4

0 回答 0