Android 做 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11348637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:06:51  来源:igfitidea点击:

Android do while loop

androidwhile-loop

提问by Peter James

Hi This my second question here. I have the following table

嗨,这是我的第二个问题。我有下表

|-----|-------|------|------|
|._id.|..INFO.|.DONE.|.LAST.|
|..1..|...A...|...N..|......|
|..2..|...B...|...Y..|..L...|<--- cursor.moveToPosition((int)_id-1);
|..3..|...C...|...Y..|......|
|..4..|...D...|...Y..|......|
|..5..|...E...|...N..|......|
|..6..|...F...|...N..|......|
|-----|-------|------|------|

I use the code:

我使用代码:

cursor = db.query(TABLE_NAME, new String[]{INFO,DONE,LAST},null,null,null,null,null);
cursor.moveToPosition((int)_id-1);
String Yval = cursor.getString(cursor.getColumnIndex(DONE));
do
{
    cursor.moveToNext();
    Yval= cursor.getString(cursor.getColumnIndex(DONE));
}
while (Yval=="Y");
s = Yval;

I initially point the cursor to the LAST row I accessed, then I make a loop to go through the values in the DONE column, not stopping if there are Y's in the row of the column. When an N appears in the loop, the loop should stop. But it doesn't work. Yval never equals "Y". So the cursor does one 'moveToNext' and then exits the loop, because it doesn't read Yval as a "Y". (I also changed everything to integers. 1 for N, and 0 for Y, but it still didn't work) So what am I doing wrong here?

我最初将光标指向我访问的最后一行,然后我循环遍历 DONE 列中的值,如果列的行中有 Y 则不会停止。当循环中出现 N 时,循环应停止。但它不起作用。Yval 永远不等于“Y”。所以光标做了一个 'moveToNext' 然后退出循环,因为它没有将 Yval 读为“Y”。(我也把所有的东西都改成了整数。1 代表 N,0 代表 Y,但它仍然不起作用)那么我在这里做错了什么?

回答by Simon Dorociak

So you have to use equals()method if you want to compare Strings

所以equals()如果你想比较,你必须使用方法Strings

while (Yval.equals("Y"));

You should know that:

你应该知道:

  • ==tests for reference equality.
  • equalstests for value equality.
  • ==测试参考相等性。
  • equals值相等的测试。

So you want to test if YvalStringhas Yvalue so you have to use equals()method.

所以你想测试YvalString是否有Y值,所以你必须使用equals()方法。

You approach doesn't work bacause:

你的方法不起作用,因为:

String data = "lorem";
data == "lorem" ==> FALSE
data.equals("lorem") == TRUE


Also make sure that your Cursorhas valid rowso you need to add to condition also cursor.moveToNext()so


还要确保你Cursor有有效的行,所以你需要添加到条件中cursor.moveToNext()所以

cursor.moveToNext() && (Yval.equals("Y")

also you need to treat cursor.moveToPosition((int)_id-1)so add it to condition.

您还需要治疗,cursor.moveToPosition((int)_id-1)因此将其添加到条件中。

回答by Sam

I recommend changing a few things:

我建议改变一些事情:

if(cursor.moveToPosition((int) _id - 1)) {
    int doneIndex = cursor.getColumnIndex(DONE);
    String Yval;
    do {
        Yval = cursor.getString(doneIndex);
    } while(Yval.equals("Y") && cursor.moveToNext());
}
  • You should check if a row exists at position _id - 1since the SQLite _idis a unique id, not the position of a row in a Cursor.
  • You only need to fetch the index of the DONEcolumn once, simply store it in a local variable.
  • As deceiver mentioned, String are tested with equals()and similar methods.
  • You need consider what happens if all of the rows are "DONE", then you must stop trying to read the Cursor before an out of boundsexception is thrown.
  • 您应该检查位置是否存在一行,_id - 1因为 SQLite_id是唯一的 id,而不是光标中行的位置。
  • 您只需要获取DONE列的索引一次,只需将其存储在局部变量中。
  • 正如欺骗者所提到的,String 是用equals()类似的方法测试的。
  • 您需要考虑如果所有行都为“DONE”会发生什么,那么您必须在抛出越界异常之前停止尝试读取 Cursor 。