在我的应用程序中,我有一个名为“出勤”的表,我希望在其中执行插入、更新和显示操作。
现在,单击显示按钮时显示的是一个空表,而不是包含所有插入条目的表。有人可以帮助我吗?
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();
//insert operation
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adb.insertEntry(roll,name,percent);
}
});
//update operation
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adb.updateEntry(roll,name,percent);
}
});
//show operation in table format
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=adb.getAll();
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Roll Number");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("Name");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null, Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
textView5.setText("Percent");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null, Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("ROLLNO")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("STUNAME")));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("PERCENTAGE")));
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
出勤数据基础适配器:
public class AttendanceDataBaseAdapter
{
public final String DATABASE_NAME="grietportal.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;
}
//insert
public void insertEntry(String roll,String name,String percent)
{
db=dbhelper.getWritableDatabase();
ContentValues newcontent=new ContentValues();
newcontent.put("ROLLNO",roll);
newcontent.put("STUNAME", name);
newcontent.put("PERCENTAGE",percent);
db.insert("ATTENDANCE", null, newcontent);
db.close();
}
//update
public void updateEntry(String roll,String name,String percent)
{
db=dbhelper.getWritableDatabase();
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});
db.close();
}
//show
public Cursor getAll() {
db=dbhelper.getWritableDatabase();
return db.query("ATTENDANCE", new String[] {"ROLLNO","STUNAME",
"PERCENTAGE"}, null, null, null, null, null);
}
}