单击“显示”按钮时显示sqlite表的内容

时间:2013-07-29 12:28:01

标签: android database sqlite

package com.vidhi.grietportal;
import android.app.Activity;
import android.database.Cursor;
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




        }
});
}
}

单击时的第三个按钮,我希望它以漂亮的列表或表格格式显示表“出勤”的内容。我对android中的数据库是全新的,并且难以编写代码! 有人可以帮帮我吗?

这是我的适配器类

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 show() {



 }
 }

1 个答案:

答案 0 :(得分:0)

替换此

  b3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Cursor mCursorGetDetail = adb.rawQuery("Select * From ATTENDANCE",null);
        mCursorGetDetail.moveToFirst();

            while (!mCursorGetDetail.isAfterLast()) {

            String ROLLNO=mCursorGetDetail.getString(mCursorGetDetail.getColumnIndex("ROLLNO"));
            String STUNAME=mCursorGetDetail.getString(mCursorGetDetail.getColumnIndex("STUNAME"));
            String PERCENTAGE=mCursorGetDetail.getString(mCursorGetDetail.getColumnIndex("PERCENTAGE"));

            //Do Whatever You Want  With Data
              mCursorGetUserDetail.moveToNext();
            }
    });