2
我创建了一个函数,用于更新我需要在多个视图上使用的数据库中的某些值。现在每个类都有自己的代码副本。基本上我读了值,然后想更新文本视图。如何从另一个类调用引用(TextView)findViewById
我为DisplayTop做了一个类,它完成了这项工作,并且它在本地非常棒。但不知何故,我需要通过我认为的函数发送(TextView)findViewById。我希望我说得对。那么对此的调用以及如何解决(TextView)findViewById。谢谢你的帮助!
public void DisplayTop2()
{
int UserID = 1;
String D=Fun.getCurrentDateString();
String Sql;
Double a = 0.0;
Double b = 0.0;
SQLiteDatabase Db;
String myPath = DB_PATH + DB_NAME;
Db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Sql =" SELECT a,b " +
" FROM test " +
" where Dte = '" + D + "' and UserID = " + UserID;
try {
Cursor c = Db.rawQuery(Sql, null);
if (c != null) {
if (c.moveToFirst()) {
do {
a += (c.getDouble(c.getColumnIndex("a")));
b += (c.getDouble(c.getColumnIndex("b")));
}while (c.moveToNext());
}
}
c.close();
} catch (Exception e) {
}
Db.close();
// Here is where the problem is. The real app has 5 fields.
// I can pass the R.id.a
// But how do I pass the reference to findViewById to the activity that called this
TextView tvt4 = (TextView)findViewById(R.id.a);
D = Fun.round(a,1,BigDecimal.ROUND_HALF_UP);
tvt4.setText(D) ;
tvt4 = (TextView)findViewById(R.id.b);
D = Fun.round(b,1,BigDecimal.ROUND_HALF_UP);
tvt4.setText(D) ;
};
另一个骗子的方法是从活动中传递上下文,但是你的解决方案要干净得多。 – sgarman 2011-01-12 16:30:22