显示sqlite数据库的内容

时间:2013-07-31 11:29:27

标签: android database sqlite

我有一个更新页面,允许插入,更新和显示数据库的内容。 此文件的xml布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bckg1"
android:gravity="top"
android:orientation="vertical" >

<TextView
    android:id="@+id/gotomenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="46dp"
    android:layout_marginRight="18dp"
    android:text="Go to Main Menu"
    android:textColor="#221778"
    android:textSize="15sp" />

<EditText
    android:id="@+id/entryforroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/rollno"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/attendancepercent"
    android:ems="10"
    android:inputType="textPersonName"
    android:hint="Enter Roll Number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/rollno"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="58dp"
    android:text="Roll Number:"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#221778"
    android:textStyle="bold" />

<Button
    android:id="@+id/show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/gotomenu"
    android:layout_toLeftOf="@+id/gotomenu"
    android:text="Show"
    android:textColor="#221778"
    android:textStyle="bold" 
    android:onClick="getall"
    />

<Button
    android:id="@+id/insert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/show"
    android:layout_alignLeft="@+id/name"
    android:layout_marginBottom="36dp"
    android:text="Insert"
    android:textColor="#221778"
    android:textStyle="bold"
     />

<Button
    android:id="@+id/update"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/insert"
    android:layout_alignBottom="@+id/insert"
    android:layout_alignRight="@+id/gotomenu"
    android:text="Update"
    android:textColor="#221778"
    android:textStyle="bold" 
    />

<TextView
    android:id="@+id/attendancepercent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/insert"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="23dp"
    android:text="Attendance %:"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#221778"
    android:textStyle="bold" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/rollno"
    android:layout_marginTop="24dp"
    android:text="Name:"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#221778"
    android:textSize="22sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/entryforname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/name"
    android:layout_alignLeft="@+id/show"
    android:ems="10"
    android:inputType="textPersonName"
    android:hint="Enter Name" />

<EditText
    android:id="@+id/entryforpercentage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/attendancepercent"
    android:layout_toRightOf="@+id/show"
    android:ems="10"
    android:hint="Enter %" />

现在这三个按钮执行插入,更新和显示的操作。这个的java代码如下:

    package com.vidhi.grietportal;

    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    public class Update extends Activity {


    Button b1,b2,b3;
    EditText e1,e2,e3;
    AttendanceDataBaseAdapter adb;
    SQLiteDatabase db;
    DataBaseHelper dbh;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update);


    adb= new AttendanceDataBaseAdapter(this);
    adb=adb.open();

    b1=(Button)findViewById(R.id.insert);
    b2=(Button)findViewById(R.id.update);
    b3=(Button)findViewById(R.id.show);

    e1=(EditText)findViewById(R.id.entryforroll);
    e2=(EditText)findViewById(R.id.entryforname);
    e3=(EditText)findViewById(R.id.entryforpercentage);

    final String roll=e1.getText().toString();
    final String name=e2.getText().toString();
    final String percent=e3.getText().toString();


    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub



            adb.insertEntry(roll,name,percent);

        }
    });

    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            adb.updateEntry(roll,name,percent);

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent ine=new Intent(v.getContext(),DisplayAtt.class);
    startActivityForResult(ine, 0);
}
    });
    }
    }

适配器类如下:

package com.vidhi.grietportal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AttendanceDataBaseAdapter 
{

public final String DATABASE_NAME="griet.db";
static final int DATABASE_VERSION=2;
static final int NAME_COLUMN=1;
public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbhelper;
public AttendanceDataBaseAdapter(Context _context)
{
     context=_context;
     dbhelper=new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public AttendanceDataBaseAdapter open() throws SQLException
{
    db=dbhelper.getWritableDatabase();
    return this;
}
public void close()
{
     db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
    return db;
}
public void insertEntry(String roll,String name,String percent)
{
    ContentValues newcontent=new ContentValues();
    newcontent.put("ROLLNO",roll);
    newcontent.put("STUNAME", name);
    newcontent.put("PERCENTAGE",percent);

    db.insert("ATTENDANCE",null, newcontent);


}

public void updateEntry(String roll,String name,String percent)
{
    ContentValues newupdate=new ContentValues();
    newupdate.put("ROLLNO", roll);
    newupdate.put("STUNAME",name);
    newupdate.put("PERCENTAGE", percent);

    String where="ROLLNO=?";
    db.update("ATTENDANCE", newupdate, where, new String[] {roll});

}

public Cursor getAll()  {

    return db.query("ATTENDANCE", new String[] {"ROLLNO","STUNAME",
            "PERCENTAGE"}, null, null, null, null, null);
}


}

Databasehelper类如下:

package com.vidhi.grietportal;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DataBaseHelper extends SQLiteOpenHelper {

private static final String TABLE1 = "login";
private static final String TABLE2 = "ATTENDANCE";
private static final String COLUMN_ID = "id";
private static final String COLUMN_TITLE1 = "username";
private static final String COLUMN_TITLE2 = "password";
private static final String COLUMN_ID1 = "id1";
private static final String COLUMN_TITLE3 = "ROLLNO";
private static final String COLUMN_TITLE4 = "STUNAME";
private static final String COLUMN_TITLE5 = "PERCENTAGE";


public static final String TABLE1_CREATION = "create table " + TABLE1 + " (" + COLUMN_ID + "     integer primary key autoincrement, " + COLUMN_TITLE1 + " TEXT,"+ COLUMN_TITLE2 +" TEXT"+")";
public static final String TABLE2_CREATION = "create table " + TABLE2 + " (" + COLUMN_ID1 + " integer primary key autoincrement, " + COLUMN_TITLE3 + " TEXT,"+ COLUMN_TITLE4 +" TEXT,"+   COLUMN_TITLE5 +" TEXT"+")";


public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE1_CREATION);
    db.execSQL(TABLE2_CREATION);

}

@Override
public void onUpgrade(SQLiteDatabase db, int paramInt1, int paramInt2) {
    db.execSQL("DROP TABLE " + TABLE1_CREATION + ";");
    db.execSQL("DROP TABLE " + TABLE2_CREATION + ";");

    onCreate(db);

}
}

DisplayAtt类如下:

package com.vidhi.grietportal;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;


public class DisplayAtt extends Activity {

AttendanceDataBaseAdapter adb;
DataBaseHelper dbh1;
TextView tv1=(TextView)findViewById(R.id.textView1999);


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displayatt);

    adb.open();
    //db.fetchAllNotes();
    Cursor c = adb.getAll();
    int i=1;
    if (c.moveToFirst())   {        
        do {
            DisplayContact(c,i++);
        } while (c.moveToNext());
    }
    c.close();
    adb.close();
} 

public void DisplayContact(Cursor c,int row )   {            

     String name11 = c.getString(1) + c.getString(2) + c.getString(3);           
     tv1.setText(name11 );

  }
   }

我们希望使用DisplayAtt类显示Attendance表的内容。但代码给了我一个空指针异常。请帮忙!

1 个答案:

答案 0 :(得分:0)

我不知道是不是全部,但你还没有初始化你的AttendanceDataBaseAdapter。在onCreate()

中必须是这样的
   adb = new AttendanceDataBaseAdapter(this);

之前打电话

   adb.open();