【问题标题】:Two return statements in SQLite helper class not permitted不允许 SQLite 助手类中的两个返回语句
【发布时间】:2018-04-08 03:31:11
【问题描述】:

要理解我的问题,我们需要解释项目设计。我们有一个简单的 MVP 概念,其中一个 DB 有 3 列 id 名称 cost(int) 使用 DBHelper 和模型类,以及一个由 RecyclerAdapter 支持的 ListActivity 以及一个 DetailsActivity,它显示 DB 中的所有元素。 ListActivity 中的项目被选中。启动活动 MainActivity 将允许用户运行许多查询标准公平显示从 ListActivity 调用后填充在 DBHelper 中的 ArrayList 中的所有数据这是 DBHelper 有此语句发送回 dbList 的重要部分到 ListActivity "return modelList" 所以我们决定创建一个查询,对成本列中的值求和,然后将总和返回给 MainActivity。 DBHelper 对两条 RETURN 语句非常不满意。因为对 DBHelper 的调用是从 ListActivity 进行的,所以我们试图通过从 MainActivity 直接调用来绕过这一点。没有办法 DBHelper 说一个 RETURN 语句。我的问题是如何从 DBHelper 返回总和值?查询有效,我们将在下面发布从 Main 调用的代码。

    public sumALL(){
    Cursor cursorA =null;
    db = this.getWritableDatabase();
    String q = "SELECT SUM(cost) FROM " + TABLE_INFO;
    cursorA = db.rawQuery(q,null);
    if(cursorA.moveToFirst()){

        totalPrice = cursorA.getInt(0);
    }

    cursorA.close();
    db.close();
    return totalPrice;

【问题讨论】:

  • 每个方法都应该有只有一个返回语句!
  • @ModularSynth 是的,我同意这一点,但第二种方法只是其中之一。我们不涉及填充 dbList 的方法我们认为是因为 Cost 列在模型类中,这就是 DBHelper 抱怨的原因
  • the second method ... 哪一个?我只看到 一个 方法。
  • 如何从 void 方法返回一个值(在本例中为 totalPrice)?
  • 此外,return() 语句之后的所有语句都不会执行。 return() 结束游戏。

标签: android sqlite return android-sqlite


【解决方案1】:

好的,这并不容易,也不简单,我将尝试解释所需的步骤。我有一个简单的 MVP 设计来测试你可能会保留一个来测试新想法。首先,您需要在调用 DBHelper 类 NO INTENT 的 Activity 中添加此代码以访问 DBHelper

这是 MainActivity 中调用 DBHelper 中方法的代码

static Context contextX;

contextX = this.getBaseContext();

public void onSUM(View view){
Toast.makeText( MainActivity.this, "Sum the Cost", Toast.LENGTH_LONG 
 ).show();
helper = new DBHelper(this);
helper.sumALL();

}

现在 DBHelper 中的代码带有捆绑 totalPrice 的 Intent

    public void sumALL(){
    Cursor cursorA =null;
    db = this.getWritableDatabase();
    String q = "SELECT SUM(cost) FROM " + TABLE_INFO;
    cursorA = db.rawQuery(q,null);
    if(cursorA.moveToFirst()){

        totalPrice = cursorA.getInt(0);
    }

    cursorA.close();
    db.close();
    //return totalPrice;

    Bundle extrasB = new Bundle();
    extrasB.putInt("BACK",totalPrice);
    contextX = MainActivity.contextX;
    Intent intentB = new Intent(contextX,ShowTotalActivity.class);

    intentB.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    // required to use intent in non Activity Class
    intentB.putExtras(extrasB);
    contextX.startActivity(intentB);

}

因为 MainActivity 是启动器,所以在该 Activity 中检索捆绑包时会遇到问题所以我们创建了一个新 Activity,正如您在 Intent 中看到的,这里是捆绑包代码的检索

    Context contextX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_total);
    etTotal =(EditText)findViewById(R.id.etTotal);
    btnBack = (Button)findViewById(R.id.btnBack);

    Intent intentB = getIntent();
    Bundle extrasB = intentB.getExtras();
    int costB = extrasB.getInt("BACK");

    DecimalFormat df = new DecimalFormat("#.00"); // Set your desired format 
    String dollarSIGN = df.format(costB/100.0);
    etTotal.setText(String.valueOf("Total of Gas   $"+dollarSIGN));

有些人可能会问为什么会有奇怪的名字 contextX 那是因为 DBHelper 有一个上下文声明只是为了避免冲突我们还必须将 totalPrice 声明为 MainActivity 中的 public static Integer totalPrice 并将其导入 DBHelper

【讨论】:

    【解决方案2】:

    虽然 Grendel 的回答会起作用,但它需要更多的代码,并且由于继承设计似乎很困难我们决定不使用 DBHelper 来运行查询,而是在 MainActivity 中执行查询以处理只有一次返回的问题在 DBHelper 中 MainActivity 中使用的代码贴在下面这个设计需要将 DBHelper 导入回 MainActivity

    import static com.<package_name>.DBHelper.TABLE_INFO;
    

    私有 DBHelper 助手; 私有 SQLiteDatabase 数据库;

    public void onSUM(查看视图){ Toast.makeText( MainActivity.this, "Sum the Cost", Toast.LENGTH_LONG ).show();

    Cursor cursor =null;
    helper = new DBHelper(this);
    db = helper.getWritableDatabase();
    String q = "SELECT SUM(cost) FROM " + TABLE_INFO;
    cursor = db.rawQuery(q,null);
    if(cursor.moveToFirst()){
    
        totalPrice = cursor.getInt(0);
    }
    
    DecimalFormat df = new DecimalFormat("#.00");
    String dollarSIGN = df.format(totalPrice/100.0);
    etTotoal.setText(String.valueOf("Total of Gas   $"+dollarSIGN));
    
    cursor.close();
    db.close();
    

    }

    【讨论】:

    • 看起来不错,是的代码更少,不确定这是否是一种好习惯如果有人能评论 DBHelper 类中的两个返回,那就太好了 好的部分是 Android 总是有两种方法来做某事
    【解决方案3】:

    对于任何查看前两个答案的人来说,是的,他们工作并且都没有解决问题,评论员和你的评论员也没有真正仔细研究 DBHelper 类中方法的构造这个 public sumALL(){ 大错特错,它应该是 public int sumALL(){ 因此将从 MainActivity 代码进行调用并将发布为 DBHelper 正确构造的 sumALL 方法

    从 MainActivity 调用代码

            helper = new DBHelper(this);
            helper.summALL();
            etLineTwo.setText(String.valueOf(totalPrice));
            DecimalFormat df = new DecimalFormat("#.00"); // Set your desired format here.
            String dollarSIGN = df.format(totalPrice/100.0);
            etLineTwo.setText(String.valueOf("of Gas   $"+dollarSIGN));
    

    带返回的 DBHelper 方法的代码

        public int summALL(){
        Cursor cursor = null;
        db = this.getWritableDatabase();
        String query = "SELECT SUM(cost) FROM " + TABLE_INFO;
        cursor = db.rawQuery(query,null);
        if(cursor.moveToFirst()){
    
            totalPrice = cursor.getInt(0);
        }
        db.close();
        cursor.close();
        return totalPrice;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多