显示存储在sqlitedatabase中的数据

时间:2013-07-08 08:27:08

标签: android

带保存和显示按钮的登录页面我想显示使用toast保存在sqlite中的数据...问题在显示代码中请帮助我...我在这里发布我的代码...我是初学者< / p>


MainActivity.java

package com.example.pagevalidation;
    import com.example.pagevalidation.*;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends Activity {

        SQLiteDatabase db;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            EditText name = (EditText) findViewById(R.id.editText1);
            String uname = name.getText().toString();

            EditText password = (EditText) findViewById(R.id.editText2);
            String pass = password.getText().toString();

        }

        public void savedata(View view) {

            EditText name = (EditText) findViewById(R.id.editText1);
            String uname = name.getText().toString();

            EditText password = (EditText) findViewById(R.id.editText2);
            String pass = password.getText().toString();

            DatabaseHandler dh = new DatabaseHandler(this);
            dh.insert(uname, pass);
            // Toast.makeText(MainActivity.class,"record inserted",3000).show();
            Toast.makeText(getBaseContext(), "Record Saved", Toast.LENGTH_LONG)
                    .show();

            // TODO Auto-generated method stub

        }

        public void displaydata() {
            DatabaseHandler dh = new DatabaseHandler(this);
    Cursor c= dh.getAll();
            if (c.moveToFirst()) {
            do {
                    Toast.makeText(this,"id: " + c.getString(0) + "\n" + "name: " + c.getString(1)+ "\n" + "password: " + c.getString(2),Toast.LENGTH_LONG).show();

            } while (c.moveToNext());

            }
            db.close();
        }


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

    }



Databasehandler class for handling database 


DatabaseHandler.java


package com.example.pagevalidation;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseHandler extends SQLiteOpenHelper  {


    private static final int DATABASE_VERSION = 1;


    private static final String DATABASE_NAME = "loginManager";


    private static final String TABLE_LOGIN = "login";


    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PASS = "password";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
         String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                    + KEY_PASS + " TEXT" + ")";
            db.execSQL(CREATE_LOGIN_TABLE); 

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

       // db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);


      //  onCreate(db);
    }

    void insert(String name,String password) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name); 
        values.put(KEY_PASS, password); 
        db.insert(TABLE_LOGIN, null, values);
        db.close(); 
    }

   /* public Cursor getAll() {
        return(getReadableDatabase()
        .rawQuery("SELECT id, name, password FROM TABLE_LOGIN",
        null));
        }*/
    public Cursor getAll()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("select * from TABLE_LOGIN", null);

        return c;

    }


   }

1 个答案:

答案 0 :(得分:0)

我看到的第一件事是你不是从任何地方调用displaydata()方法。

在onCreate()方法的末尾添加它:

onCreate(){
   ...
   displayData();
}

您是否是一名初学者,那么您应该查看有关活动如何工作的文档,基本上他们有一些方法,您可以覆盖它们以添加您的活动的行为(如onCreate())。不需要覆盖的方法(我的意思是,你创建这些方法)需要从某个地方调用。