我是Android应用程序开发的新手。最近,我通过自己开发GPA计算器来磨练自己的技能。目前,我有以下代码可以帮助我获得一个人的累积GPA
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
SQLiteDatabase sqLiteDatabase;
DataManager sqLiteHelper;
Cursor cursor;
Button Delete, Edit;
SQLiteDatabase sqLiteDatabaseObj;
String SQLiteDataBaseQueryHolder ;
TextView semester,gpa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (TextView) findViewById(R.id.textViewGPA);
groupgpa = (TextView) findViewById(R.id.textViewgroupGPA);
/*semester = (TextView) findViewById(R.id.textViewSEMESTER);*/
SQLiteDataBaseBuild();
sqLiteHelper = new DataManager(this);
SQLiteTableBuild();
double groupgpa2 =semestralgpa();
groupgpa.setText(Double.toString(groupgpa2));
double gpa2 = totalgpa();
gpa.setText(Double.toString(gpa2));
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.about:
// Do Something
Toast.makeText(getApplicationContext(),"About...",
Toast.LENGTH_SHORT).show();
return true;
case R.id.addsem:
// Do Something
startActivity(new Intent(this, ADDSEMESTERACTIVITY.class));
return true;
case R.id.allgrade:
// Do Something
startActivity(new Intent(this, DISPLAYALLGRADEACTIVITY.class));
return true;
}
return false;
}
@Override
public void onClick(View view) {
/* switch (view.getId()){
case R.id.photoalbum:
Intent i = new Intent(this, PhotoAlbum.class);
startActivity(i);
break;
}*/
}
@Override
protected void onResume() {
totalgpa();
super.onResume();
}
public void OpenSQLiteDataBase(){
sqLiteDatabaseObj = openOrCreateDatabase(DataManager.DATABASE_NAME, Context.MODE_PRIVATE, null);
}
public void SQLiteDataBaseBuild(){
sqLiteDatabaseObj = openOrCreateDatabase(DataManager.DATABASE_NAME, Context.MODE_PRIVATE, null);
}
public void SQLiteTableBuild(){
sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS "+DataManager.TABLE_NAME+"("+DataManager.Table_Column_ID+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "+DataManager.Table_Column_1_SUBJECT+" VARCHAR, "+DataManager.Table_Column_2_GRADE+" VARCHAR, "+DataManager.Table_Column_3_CREDIT+" INTEGER, "+DataManager.Table_Column_4_GCPRODUCT+" DECIMAL, "+DataManager.Table_Column_5_SEMESTER+" VARCHAR);");
}
public double totalgpa(){
double gpa = 0;
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT sum(gcproduct)/sum(credit) FROM " + DataManager.TABLE_NAME +" group by Semester", null);
if(cursor.moveToFirst()){
gpa = cursor.getDouble(0);
}
return gpa;
}
}
计算GPA的方法。
public double totalgpa(){
double gpa = 0;
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT sum(gcproduct)/sum(credit) FROM " + DataManager.TABLE_NAME +"", null);
if(cursor.moveToFirst()){
gpa = cursor.getDouble(0);
}
return gpa;
}
能够显示可用的总和。但是,如果我要添加更多值,则不会更新。 有人能告诉我我做错了什么吗?
另外,如果我按照学期的分组运行一种方法来获得学期的gpa,我只能打印出一个学期的gpa。
有问题的方法:
public double semestralgpa(){
double gpa = 0;
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT sum(gcproduct)/sum(credit) FROM " + DataManager.TABLE_NAME +" group by Semester", null);
if(cursor.moveToFirst()){
gpa = cursor.getDouble(0);
}
return gpa;
}
答案 0 :(得分:0)
您遇到的问题是在检索onResume
方法中的值后需要更新视图(TextView)。
目前你有: -
@Override
protected void onResume() {
totalgpa();
super.onResume();
}
这只检索新值。您还应该让超级电话进行业务并恢复活动。因此,而不是上述用途: -
@Override
protected void onResume() {
super.onResume();
gpa.setText(Double.toString(totalgpa));
}
这将检索该值,并将其应用于一行中的TextView。
关于: -
另外,如果我要运行一种方法来获得学期的gpa 按学期分组,我只能打印出1的gpa 学期。
听起来您想要显示分组的semestralgpa的列表,您可以使用: -
public String semestralgpa(){
StringBuilder sb = new StringBuilder();
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT sum(gcproduct)/sum(credit) FROM " + DataManager.TABLE_NAME +" group by Semester", null);
if(cursor.moveToNext()){
// Add separator between values.
if (cursor.getPosition() > 0) {
sb.append(", ");
}
sb.append(String.valueOf(cusror.getDouble(0)));
// or sb.append(cursor.getString(0));
// or sb.append(Double.toString(cursor.getDouble(0)));
}
return sb.toString;
}
请注意!以上未经过测试,因此可能包含一些错误。
然后,您可能会看到该列表只是gpa,而且没有指示他们所用的学期。因此,您可能想要/更喜欢: -
public String semestralgpa(){
StringBuilder sb = new StringBuilder();
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT sum(gcproduct)/sum(credit) AS Semestergpa, Semester FROM " + DataManager.TABLE_NAME +" group by Semester", null);
if(cursor.moveToNext()){
// Add separator between values.
if (cursor.getPosition() > 0) {
sb.append(", ");
}
sb.append(String.valueOf(cusror.getDouble(cursor.getColumnIndex("Semestergpa")));
sb.append(" for ");
sb.append(cursor.getString(cursor.getColumnIndex("Semester")));
sb.append(".");
}
return sb.toString;
}
sum(gcproduct)/sum(credit)
重命名为Semestergpa
(显示示例)使用硬编码偏移很容易导致错误,因此显示了getColumnIndex的原因。这将根据列名称获取偏移量。如果您要使用派生的偏移量,则可以使用: -
cursor = sqLiteDatabase.rawQuery("SELECT Semester, sum(gcproduct)/sum(credit) AS Semestergpa FROM " + DataManager.TABLE_NAME +" group by Semester", null);
(虽然 0 现在是学期)