下記の結果が出たため、cは、nullではないということから、selectは、できていると考えるのですが、whileでは、falseが返されます。なぜでしょうか?
アドバイスをいただけないでしょうか?どうぞよろしくお願いいたします
java
1if(c!=null){ 2Log.d("cはnullではなかった","このLogは出力された"); 3while(c.moveToNext()){ 4Log.d("whileには、はいれなかった","このLogはしゅつりょくされなかった"); 5 } 6}
java
1public class LocationActivity extends FragmentActivity implements 2 GoogleApiClient.ConnectionCallbacks, 3 GoogleApiClient.OnConnectionFailedListener, 4 LocationListener { 5 6 7 private TextView textView; 8 private String textLog = "start \n"; 9 10 // LocationClient の代わりにGoogleApiClientを使います 11 private GoogleApiClient mGoogleApiClient; 12 private boolean mResolvingError = false; 13 14 private FusedLocationProviderApi fusedLocationProviderApi; 15 16 private LocationRequest locationRequest; 17 private Location location; 18 private long lastLocationTime = 0; 19 20 ///////////////////取得したlongitudeとlatitude 21 double latitude; 22 double longitude; 23 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 Log.d("LocationActivity", "onCreate"); 31 textView = (TextView) findViewById(R.id.text_view); 32 33 /////////////////////////locationRequest 34 // LocationRequest を生成して精度、インターバルを設定 35 locationRequest = LocationRequest.create(); 36 locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 37 locationRequest.setInterval(1000); 38 locationRequest.setFastestInterval(16); 39 40 fusedLocationProviderApi = LocationServices.FusedLocationApi; 41 mGoogleApiClient = new GoogleApiClient.Builder(this) 42 .addApi(LocationServices.API) 43 .addConnectionCallbacks(this) 44 .addOnConnectionFailedListener(this) 45 .build(); 46 47 Log.d("LocationActivity", "mGoogleApiClient"); 48 49 textLog += "onCreate() \n"; 50 textView.setText(textLog); 51 52 53 Button buttonTransmission = (Button) findViewById(R.id.buttonTransmission); 54 buttonTransmission.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View v) { 57 58 MyOpenHelper helper = new MyOpenHelper(LocationActivity.this); 59 SQLiteDatabase db = helper.getWritableDatabase(); 60 61 EditText editText = (EditText) findViewById(R.id.Transmission); 62 String comment = editText.getText().toString(); 63 64 textLog += "start insert : " + comment + "\n"; 65 textView.setText(textLog); 66 67 //calendarの取得 68 Calendar calendar = Calendar.getInstance(); 69 int month = calendar.get(Calendar.MONTH) + 1; 70 int day = calendar.get(Calendar.DATE); 71 int hour = calendar.get(Calendar.HOUR); 72 int minute = calendar.get(Calendar.MINUTE); 73 int second = calendar.get(Calendar.SECOND); 74 String data = month + "/ " + day + " " + hour + ":" + minute + ": " + second; 75 76 String username = "ta"; 77 78 String sql = "insert into neardb(data,username,comment,latitude,longitude)" + 79 " values ('" + data + "','" + username + "','" + comment + "'," + latitude + "," + longitude + ");"; 80 81 Log.v("-----insertのsql-----", sql); 82 83 TextView textView; 84 String textLog = "insertします\n"; 85 textView = (TextView) findViewById(R.id.text_view); 86 87 88 Cursor c = db.rawQuery(sql, null); 89 textLog += "insertしました : " + sql + "\n"; 90 textView.setText(textLog); 91 92 c.close(); 93 db.close(); 94 95 Log.d("--FusedLocationよぶ--", "insert終了"); 96 97 startFusedLocation(); 98 } 99 }); 100 } 101 102 103 private void startFusedLocation() { 104 Log.d("fusedLocation", "よばれた"); 105 106 107 // Connect the client. 108 if (!mResolvingError) { 109 // Connect the client. 110 111 Log.d("---googleconectよぶ---", "mGoogleconnect"); 112 113 mGoogleApiClient = new GoogleApiClient.Builder(this) 114 .addApi(LocationServices.API) 115 .addConnectionCallbacks(this) 116 .addOnConnectionFailedListener(this) 117 .build(); 118 119 mGoogleApiClient.connect(); 120 121 textLog += "onStart(), connect() \n"; 122 textView.setText(textLog); 123 } else { 124 textLog += "onStart(), mResolvingError \n"; 125 textView.setText(textLog); 126 } 127 128 } 129 130 //このConnectedで、データを取得してるからこれを5分おきに行うようにしたいから 131 //これを呼び出してるところをtimerして、 132 133 @Override 134 public void onConnected(Bundle bundle) { 135 Log.d("LocationActivity", "onConnected"); 136 137 Log.d("---googleconectよばれた---", "onConnected到着"); 138 139 140 textLog += "onConnected()\n"; 141 textView.setText(textLog); 142 143 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != 144 PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != 145 PackageManager.PERMISSION_GRANTED) { 146 return; 147 } 148 149 Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient); 150 151 if (currentLocation != null && currentLocation.getTime() > 20000) { 152 location = currentLocation; 153 154 Log.d("---googlecon順調-----", "次はselectする"); 155 156 textLog += "----onConnectedした--- \n"; 157 textLog += "取得:Latitude=" + String.valueOf(location.getLatitude()) + "\n"; 158 textLog += "取得:Longitude=" + String.valueOf(location.getLongitude()) + "\n"; 159 textView.setText(textLog); 160 161 162 latitude = location.getLatitude(); 163 longitude = location.getLongitude(); 164 165 166 MyOpenHelper helper = new MyOpenHelper(this); 167 SQLiteDatabase db = helper.getReadableDatabase(); 168 169 //このメソッドは 170 double clearlatitude = 0.00002694944; 171 double clearlongitude = 0.00032899147; 172 173 String z = BigDecimal.valueOf(clearlatitude).toPlainString(); 174 String zz = BigDecimal.valueOf(clearlongitude).toPlainString(); 175 176//自分の値からこの人の値を引いてこのすうじいかならok 177 String sql = "select * from neardb ;"; 178 179 Log.v("-----selectする文-----", sql); 180 181 182 /** 183 String sql= "select data,username,comment from neardb " + 184 "where latitude - "+latitude+" <= "+z+" and " 185 +latitude+" - latitude <= "+z+ 186 " and longitude - "+longitude+" <= "+zz+" and " 187 +longitude+" - longitude <= "+zz+";"; 188 **/ 189 190 Cursor c = db.rawQuery(sql, null); 191 Log.d("---sql実行した----", "実行完了"); 192 193 194 textView.setText(textLog); 195 Log.d("movefirstの前 Positi1/", "" + c.getPosition()); 196 197 c.moveToFirst(); 198 199 Log.d("movefirstの後 Positi2/", "" + c.getPosition()); 200 201 202 if (c != null) { 203 Log.d("---c!=null あとはfor()--", "nullじゃない確定"); 204 Log.d("今の Positi3/", "" + c.getPosition()); 205 206 207 for (int i = 1; i <= c.getCount(); i++) { 208 //SQL文の結果から、必要な値を取り出す 209 210 Log.d(" forにはいった Positi4/", "" + c.getPosition()); 211 212 Log.d("いえーーーーい", "次は取得"); 213 214 215 TextView textView = (TextView) findViewById(R.id.text_view); 216 textView.setText(String.format("%s %s : %s", c.getString(0), c.getString(1), c.getString(2))); 217 218 String d = c.getString(0); 219 String e = c.getString(1); 220 String f = c.getString(2); 221 222 textLog += "データベースの c から取得: " + d + ":" + e + ":" + f + "\n"; 223 textView.setText(textLog); 224 225 226 c.moveToNext(); 227 228 } 229 c.close(); 230 db.close(); 231 232 Log.d("まずイエーイがこないってことはwhile","はじかれた,次はとくにない?"); 233 } 234 235 Log.d("if(c=null)", "だった場合とくにない"); 236} 237
// 測位開始
Button buttonStart = (Button) findViewById(R.id.button_start);
// 測位終了
Button buttonStop = (Button) findViewById(R.id.button_stop);
private void stopFusedLocation() {
protected void onStart() {
protected void onStop() {
@Override
public void onLocationChanged(Location location) {
if (c != null) { の
} else {
は字数の関係上書いてありません。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/29 08:46