質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

3回答

5798閲覧

Cursor cは、nullではないが、whileでは、falseとなります。

edoooooo

総合スコア476

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2017/04/28 11:25

下記の結果が出たため、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 {
は字数の関係上書いてありません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

Cursorがnull≠Cursorが空 です。
Cursorがnullであれば、moveToNext()した時点でNullPointerExceptionが発生します。

投稿2017/04/28 12:42

swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

edoooooo

2017/04/29 08:46

から文字("")でチェックをしていませんでした。 moveToNext()した時にNullPointerExceptionが発生するということも知らなかったです。 つまり、insertできていないないと言うことですね。ありがとうございます。
guest

0

cは、nullではないということから、selectは、できていると考えるのですが、whileでは、falseが返されます。なぜでしょうか?

何度も言いますがselectの結果が空だからです。なぜ伝わらないのでしょうか。
Cursor#getCountで確認してください。

投稿2017/04/28 12:02

編集2017/04/28 12:05
yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

edoooooo

2017/04/29 08:47

ありがとうございます。 insertしないとnullでなく、""(から文字)になるのですね。 insertできていないとのことですね。ありがとうございます。
yona

2017/04/29 08:49

なにを理解したのわかりませんが、違います。
edoooooo

2017/04/29 10:21

rawQuery()をexecSQL()と、変更したところ、 Cursorは、からでなくwhileでtrueが返されました。
guest

0

ベストアンサー

Androidアプリのデータ保存方法の一つ「SQLite」の使い方 レコード検索編 | mucchinのAndroid戦記リンク内容

次は、後判定のループ処理となってますね。

Cursor.moveToNext()は、カーソルを次のレコードへ移動させる、という意味合いです。
このmoveToNext()の戻り値は、次のレコードがあればカーソルを移動させてtrueを戻します。
次のレコードが無ければfalseを返します。
ちなみに、後判定としているのは、一番最初のレコードの処理を行う為です。

追記
ほんとだ。まだ直ってなかったのか。

ちなみに、rawQuery()はselect文専用です。

insert、update、delete等は、execSQL()メソッドを使います。
詳しくは下記をご覧下さい。

投稿2017/04/28 11:36

編集2017/04/28 13:08
toutou

総合スコア2050

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

edoooooo

2017/04/29 08:49

rawQuery()をinsertに使っていたことが全ての原因でした。 これ以前に作っていたアプリでもCursorがからとなっており、それも、rawQueryを使っているからでした。ありがとうございます。 解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問