任何人都可以帮我创建Android活动,以显示列表中的表(名为fac_info
)的所有数据,其中列表中的每一列都有两个按钮,其中一个用于更新,另一个用于删除数据?
这是我的数据库适配器:
package com.ayaan;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DBAdapter {
public static final String KEY_ROWID = "_id";// row id
public static final String KEY_FACNAME = "Fname";// Faculty name
public static final String KEY_PASSWORD = "Password";// password
public static final String KEY_CODE = "Code";// subject code or name
public static final String KEY_PHONE = "Phone";// phone
public static final String KEY_USN = "Usn";// usn or student name
public static final String KEY_SNAME = "Sname";// name or student ID
public static final String KEY_ATTEND = "Attended";// attending
public static final String KEY_MISSED = "Missed";// missing
public static final String KEY_CHECKBOX = "Check";// check box
public static final String KEY_TOTAL = "Total";// finding total date
public static final String KEY_ADMIN = "Admin";//
public static final String KEY_PASS1 = "Pass1";
public static final String KEY_PASS2 = "Pass2";
private static final String TAG = "STUDB";
private static final String DATABASE_NAME1 = "Student1";// DB Name
private static final String DATABASE_TABLE1 = "fac_info";// faculty table
private static final String DATABASE_TABLE2 = "stu_info";// student info
// table
private static final String DATABASE_TABLE3 = "class_total";//
private static final String DATABASE_TABLE4 = "admin_values";
private static final int DATABASE_VERSION = 1;// DB version
private static final String DATABASE_CREATE = "create table fac_info (_id integer primary key autoincrement, Fname text not null, Password text not null, Code text not null);";
private static final String DATABASE_CREATE1 = "create table stu_info (_id integer primary key autoincrement, Sname text not null, Usn text not null, Code text not null, Attended integer, Missed integer, Phone text not null, Percent integer);";
private static final String DATABASE_CREATE2 = "create table class_total (_id integer primary key autoincrement, Code text not null,Total integer);";
private static final String DATABASE_CREATE3 = "create table admin_values (_id integer primary key autoincrement, Admin text not null, Pass1 text not null, Pass2 text not null);";
private DatabaseHelper DBHelper;//* this is instance of the class
private final Context context;//*this is context of the class
private SQLiteDatabase db;//*this is for SQLite
public DBAdapter(Context ctx) {//*(DBAdapter) is the name of our java class
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME1, null, DATABASE_VERSION);
//*this above just initializing variable
//*super(context, name of a DB, by default is(null), DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {//*we have to have this method, because of DatabaseHelper
long x;
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE1);
db.execSQL(DATABASE_CREATE2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//*we have to have this method, because of DatabaseHelper
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS PESITMSESTUDENT");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
Log.i(TAG, "opening the database");
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
Log.i(TAG, "closing the database");
DBHelper.close();
}
// ---insert a title into the database---
public long insertRecord(String fname, String pass, String code) {
long x;
Log.i(TAG, "INSERTING A FACULTY RECORD");
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_FACNAME, fname);
initialValues.put(KEY_PASSWORD, pass);
initialValues.put(KEY_CODE, code);
x = db.insert(DATABASE_TABLE1, null, initialValues);
return x;
}
public long insertStudentRecord(String sname, String usn, String code,
int att, int miss, String phone) {
long x;
Log.i(TAG, "INSERTING A STUDENT RECORD");
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SNAME, sname);
initialValues.put(KEY_USN, usn);
initialValues.put(KEY_CODE, code);
initialValues.put(KEY_ATTEND, att);
initialValues.put(KEY_MISSED, miss);
initialValues.put(KEY_PHONE, phone);
x = db.insert(DATABASE_TABLE2, null, initialValues);
return x;
}
public long insertClass(String Code, int Count) {
long x;
Log.i(TAG, "INSERTING A COUNT RECORD");
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CODE, Code);
initialValues.put(KEY_TOTAL, Count);
x = db.insert(DATABASE_TABLE3, null, initialValues);
return x;
}
// ---retrieves all the student records---
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE1, new String[] { KEY_ROWID, KEY_FACNAME,
KEY_PASSWORD, KEY_CODE }, null, null, null, null, null);
}
public Cursor getAllRecordsBy() {
return db.query(DATABASE_TABLE2, new String[] { KEY_ROWID, KEY_SNAME,
KEY_USN, KEY_CODE, KEY_ATTEND, KEY_MISSED, KEY_PHONE }, null,
null, null, null, null);
}
// ---retrieves a particular student record---
public Cursor getRecordByName(String code) throws SQLException {
Log.i("Ayaan", "GETTING STUDENT RECORD");
Cursor mCursor = db.query(true, DATABASE_TABLE1, new String[] {
KEY_ROWID, KEY_FACNAME, KEY_PASSWORD, KEY_CODE }, KEY_CODE
+ "=" + "\"" + code + "\"", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getCount(String code) throws SQLException {
Log.i("Ayaan", "GETTING STUDENT RECORD");
Cursor mCursor = db.query(true, DATABASE_TABLE3, new String[] {
KEY_ROWID, KEY_CODE, KEY_TOTAL }, KEY_CODE + "=" + "\"" + code
+ "\"", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getRecordByUsn(String Code, String usn) throws SQLException {
Log.i("Ayaan", "getRecordByUsn()");
Cursor mCursor;
mCursor = db.query(DATABASE_TABLE2,
new String[] { KEY_ROWID, KEY_SNAME, KEY_USN, KEY_CODE,
KEY_ATTEND, KEY_MISSED, KEY_PHONE }, KEY_CODE + "=?"
+ " AND " + KEY_USN + "=?", new String[] { Code, usn },
null, null, null);
if (mCursor != null) {
Log.i("Ayaan", "cursor not null");
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getRecordBycode(String code) throws SQLException {
Log.i("Ayaan", "GETTING STUDENT RECORD");
Cursor mCursor = db.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SNAME, KEY_USN, KEY_CODE, KEY_ATTEND,
KEY_MISSED, KEY_PHONE }, KEY_CODE + "=" + "\"" + code + "\"",
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getRecordTotal(String code) throws SQLException {
Log.i("Ayaan", "GETTING STUDENT RECORD");
Cursor mCursor = db.query(true, DATABASE_TABLE3, new String[] {
KEY_ROWID, KEY_CODE, KEY_TOTAL }, KEY_CODE + "=" + "\"" + code
+ "\"", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void update(String Name,String phone,String code){
db.execSQL("Update "+DATABASE_TABLE2+" set "+KEY_SNAME+"="+Name+","+KEY_PHONE+"="+phone+" where "+KEY_CODE+"="+code);
}
public boolean checkAvailability(String code,String Name){//unic
Cursor c=db.query(DATABASE_TABLE2, new String[]{KEY_SNAME},KEY_CODE+"=?" , new String[]{code}, null, null, null);
c.moveToFirst();
if(c.getString(c.getColumnIndex(KEY_SNAME)).equals(Name)){
return false;
}
else
return true;
}
public int getTotalBySCODE(String SCODE){
Cursor c=db.query(DATABASE_TABLE3, new String[]{KEY_TOTAL}, "Code=?", new String[]{SCODE}, null, null, null);
c.moveToNext();
int total=Integer.parseInt(c.getColumnName(c.getColumnIndex(KEY_TOTAL)));
return total;
}
public Cursor getRecordById(int id) throws SQLException {
Log.i("Ayaan", "GETTING STUDENT RECORD by id");
Cursor mCursor = db.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SNAME, KEY_USN, KEY_CODE, KEY_ATTEND,
KEY_MISSED, KEY_PHONE }, KEY_ROWID + "=" + id, null, null,
null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void updateattend(int id, int new_att) throws SQLException {
ContentValues values = new ContentValues();
values.put(KEY_ATTEND, new_att);
db.update(DATABASE_TABLE2, values, KEY_ROWID + "=" + id, null);
}
public void updatemissed(int id, int new_miss) throws SQLException {
ContentValues values = new ContentValues();
values.put(KEY_MISSED, new_miss);
db.update(DATABASE_TABLE2, values, KEY_ROWID + "=" + id, null);
}
public void updatecount(int id, int count) throws SQLException {
ContentValues values = new ContentValues();
values.put(KEY_TOTAL, count);
db.update(DATABASE_TABLE3, values, KEY_ROWID + "=" + id, null);
}
public void deleteStudentRecord(int id) {
Log.i("ROOPA", "Trying To Delete Record");
db.delete(DATABASE_TABLE2, KEY_ROWID + "=" + id, null);
}
}
答案 0 :(得分:0)
您需要做的第一件事是构建将填充列表的对象。这将包含数据库列字符串和两个按钮,一个用于更新,另一个用于删除。我会从像这样的somenthing开始(在这个例子中,填充列表的每个对象都有一个图像和一个文本,你应该取出图像并放入两个按钮:
public class TitleNavigationAdapter extends BaseAdapter {
private ImageView imgIcon;
private TextView txtTitle;
private ArrayList<SpinnerNavItem> spinnerNavItem;
private Context context;
public TitleNavigationAdapter(Context context,
ArrayList<SpinnerNavItem> spinnerNavItem) {
this.spinnerNavItem = spinnerNavItem;
this.context = context;
}
@Override
public int getCount() {
return spinnerNavItem.size();
}
@Override
public Object getItem(int index) {
return spinnerNavItem.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.spinner_dropdown_queues, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
imgIcon.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.spinner_dropdown_queues, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
}
public class SpinnerNavItem {
private String title;
private int icon;
public SpinnerNavItem(String title, int icon){
this.title = title;
this.icon = icon;
}
public String getTitle(){
return this.title;
}
public int getIcon(){
return this.icon;
}
}
那么在这种情况下你的列表类型将是SpinnerNavItem。尝试从这开始尝试,我会帮助你, 此致