如何从另一个类调用引用 (TextView)findViewById

发布于 2024-10-12 06:38:52 字数 1363 浏览 1 评论 0原文

我创建了一个函数,可以更新数据库中的一些值,我需要在多个视图上使用这些值。现在每个类都有自己的代码副本。基本上我读取了这些值,然后想要更新文本视图。

我为 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 )  ;



 };

I have created a function that updates some values from a database that I need to use on multiple views. Right now each class has its own copy of the code. Basically I read the values and then want to update the text views.

I made a class for the DisplayTop which does the work, and locally its great. But somehow I need to get the (TextView)findViewById sent over the the function I think. I hope I am saying this right. So what is the call to this and how to I resolved the (TextView)findViewById. thanks for any help!

 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 )  ;



 };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2024-10-19 06:38:52

我想我会尝试将所有 UI 更新保留在我的 Activity 中,而不是传递 TextView 对象。为什么不创建一个 Bean 来填充并返回到调用活动?类似于:

public class Mybean {
    private String fieldA;
    private String fieldB;
    // create constructor and getters and setters here
}

然后在数据库代码中填充并返回 bean。

public MyBean DisplayTop2() {
    ...
    MyBean bean = new MyBean();
    bean.setFieldA(Fun.round( a,1,BigDecimal.ROUND_HALF_UP));
    bean.setFieldB(Fun.round( b,1,BigDecimal.ROUND_HALF_UP));
    return bean;
}

然后在您的 Activity 中填充 UI:

...
MyBean bean = MyDBClass.DisplayTop2();
TextView txtA = (TextView)findViewById(R.id.mytextviewA);
TextView txtB = (TextView)findViewById(R.id.mytextviewB);
txtA.setText(bean.getFieldA());
txtB.setText(bean.getFieldB());

这将使您的所有 UI 代码与数据库代码分开。

I think I would try and keep all the UI updates within my Activity, rather than passing around TextView objects. Why not create a Bean to populate and return to the calling activity? Something like:

public class Mybean {
    private String fieldA;
    private String fieldB;
    // create constructor and getters and setters here
}

Then populate and return the bean in your database code.

public MyBean DisplayTop2() {
    ...
    MyBean bean = new MyBean();
    bean.setFieldA(Fun.round( a,1,BigDecimal.ROUND_HALF_UP));
    bean.setFieldB(Fun.round( b,1,BigDecimal.ROUND_HALF_UP));
    return bean;
}

Then populate the UI in your Activity:

...
MyBean bean = MyDBClass.DisplayTop2();
TextView txtA = (TextView)findViewById(R.id.mytextviewA);
TextView txtB = (TextView)findViewById(R.id.mytextviewB);
txtA.setText(bean.getFieldA());
txtB.setText(bean.getFieldB());

This keeps all your UI code separate from your DB code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文