显示的值为null

时间:2014-04-08 12:27:52

标签: android sqlite

我创建了一个简单的应用程序来学习android中的sqlite。 但是当我点击showdata按钮时,我得到空值

MainDB.java

package com.example.logintp;

    import java.util.List;

    import android.R.layout;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    import com.example.logintp.R.id;

    public class MainDB extends Activity {
        TextView name, password,show1;
        EditText etname, etpassword;
        Button save,show;
        String s1, s2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final DbClass db = new DbClass(this);
            name = (TextView) findViewById(R.id.name);
            password = (TextView) findViewById(R.id.password);
            show1=(TextView) findViewById(R.id.show);
            etname = (EditText) findViewById(R.id.etName);
            etpassword = (EditText) findViewById(id.etPassword);
            save = (Button) findViewById(R.id.save);
            show=(Button) findViewById(R.id.Display);
            show.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String data=db.showAll();
                    show1.setText(data);

                }
            });
            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    s1 = etname.getText().toString();
                    s2 = etpassword.getText().toString();
                    long id = db.insertData(new Login(s1, s2));
                    if (id < 0) {
                        CommonFunctions.display(MainDB.this, "unsucessful");

                    }else{
                        CommonFunctions.display(MainDB.this, "sucessful");
                    }

                }
            });


        }
    }

DataBaseHandler类

package com.example.logintp;

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

public class DbClass extends SQLiteOpenHelper {

private static final String db_name="LoginDetails";
private static final String table_name="Login";
private static final String name="name";
private static final String password="password";
private static final int db_version=1;
private static final String Create_Table=" CREATE TABLE " + table_name + "(" +name + " VARCHAR(200)," +password+ " VARCHAR(200)" + " ) ";


    public DbClass(Context context){
        super(context, db_name, null, db_version);
    }
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(Create_Table);

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

public long insertData(Login l){

    SQLiteDatabase db=getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(name, l.getName());
    cv.put(password, l.getPassword());
    long id=db.insert(table_name, null, cv);
    db.close();
    return id;
}

public String showAll(){
    String[] coloums= {name , password};
    StringBuffer b=new StringBuffer();
    SQLiteDatabase db=getWritableDatabase();
    Cursor cursor=db.query(table_name, coloums, null, null, null, null, null);

    while(cursor.moveToNext()){
        //Login login=new Login();
        String name1=cursor.getString(0);
        String password1=cursor.getString(1);
        b.append(name1+" "+password1+ "/n");
    }

    return b.toString();
}
}

我创建了一个简单的应用程序来学习android中的sqlite。 但是当我点击showdata按钮时,我得到空值

0 个答案:

没有答案