0

这是出勤数据库适配器类!这提供了我们需要在按钮点击时执行的更新和插入代码!

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

    public class AttendanceDataBaseAdapter
    {

    public final String DATABASE_NAME="login.db";
    static final int DATABASE_VERSION=1;
    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 void show()
 {
  db.execSQL("SELECT * from ATTENDANCE");
 }

 }

这是包含更新页面布局的 .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" 
        />

    <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 %" />

</RelativeLayout>

Update.java 文件:我只是用来详细说明要做什么!

package com.vidhi.grietportal;

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;

public class Update extends Activity {


    Button b1,b2,b3;
    EditText e1,e2,e3;
    AttendanceDataBaseAdapter adb;
    @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 arg0) {
                // TODO Auto-generated method stub
                adb.show();
            }
        });


    }


}

DataBaseHelper.java :(这对我的应用程序中的两个表都是通用的)

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 + " varchar,"+COLUMN_TITLE2+"varchar"+")";
    public static final String TABLE2_CREATION = "create table " + TABLE2 + " (" + COLUMN_ID1 + " integer primary key autoincrement, " + COLUMN_TITLE3 + " varchar,"+COLUMN_TITLE4+"varchar,"+COLUMN_TITLE5+"varchar"+")";


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

    }
}

有人可以帮帮我吗?我也无法在文件资源管理器中打开数据文件夹!

4

0 回答 0