前提・実現したいこと
Java初心者のため、至らないところもあるかと思いますがお願いいたします。
現在、AndroidStudioで、位置情報を取得するアプリを作成しています。
そこで、こちらのサイトを参考にしております。
上記のサイトで関数onLocationChanged部分が、実際に取得している部分になります。
私のソースだと関数saveListの下の方です。
この関数には、引数locarionがあります。この値はどこから取得しているのでしょうか?
関数saveListを他から呼び出したいのですが、引数に何を設定すれば良いかわかりません。
お分かりの方、ご教授いただけますようお願いいたします。
該当のソースコード
java
1package com.example.may.favorite; 2 3import android.annotation.SuppressLint; 4import android.content.Context; 5import android.content.Intent; 6import android.os.Bundle; 7import android.support.v7.app.AppCompatActivity; 8import android.view.Menu; 9import android.view.MenuItem; 10import android.view.View; 11import android.view.inputmethod.InputMethodManager; 12import android.widget.Button; 13import android.widget.EditText; 14import android.widget.RadioGroup; 15import android.widget.TextView; 16import android.widget.Toast; 17import android.location.Location; 18import android.location.LocationListener; 19import android.location.LocationManager; 20 21/** 22 * メイン画面に関連するクラス 23 * MainActivity 24 */ 25public class MainActivity extends AppCompatActivity implements 26 RadioGroup.OnCheckedChangeListener { 27 28 private EditText mEditText01ShopNm; // 店名 29 private EditText mEditText01Price; // 単価 30 31 private TextView mText01Kome01; // 店名の※印 32 private TextView mText01Kome04; // 単価の※印 33 34 private Button mButton01Regist; // 登録ボタン 35 private Button mButton01Show; // 表示ボタン 36 37 private RadioGroup mRadioGroup01Show; // 選択用ラジオボタングループ 38 39 private Intent intent; // インテント 40 private LocationManager manager = null; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 47 // GPSサービス取得 48 manager = (LocationManager)getSystemService(LOCATION_SERVICE); 49 findViews(); // 各部品の結びつけ処理 50 51 init(); //初期値設定 52 53 // ラジオボタン選択時 54 mRadioGroup01Show.setOnCheckedChangeListener(this); 55 56 // 登録ボタン押下時処理 57 mButton01Regist.setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 61 // キーボードを非表示 62 InputMethodManager inputMethodManager = 63 (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 64 inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 65 66 // DBに登録 67 saveList(); 68 69 } 70 }); 71 72 // 表示ボタン押下時処理 73 mButton01Show.setOnClickListener(new View.OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 if (intent != null) { 77 startActivity(intent); // 各画面へ遷移 78 } else { 79 Toast.makeText(MainActivity.this, "ラジオボタンが選択されていません。", Toast.LENGTH_SHORT).show(); 80 } 81 82 } 83 }); 84 } 85 86 87 /** 88 * 各部品の結びつけ処理 89 * findViews() 90 */ 91 private void findViews() { 92 93 mEditText01ShopNm = (EditText) findViewById(R.id.editText01ShopNm); // 店名 94 mEditText01Price = (EditText) findViewById(R.id.editText01Price); // 単価 95 96 mText01Kome01 = (TextView) findViewById(R.id.text01Kome01); // 店名の※印 97 mText01Kome04 = (TextView) findViewById(R.id.text01Kome04); // 単価の※印 98 99 mButton01Regist = (Button) findViewById(R.id.button01Regist); // 登録ボタン 100 mButton01Show = (Button) findViewById(R.id.button01Show); // 表示ボタン 101 102 mRadioGroup01Show = (RadioGroup) findViewById(R.id.radioGroup01); // 選択用ラジオボタングループ 103 104 } 105 106 /** 107 * 初期値設定 (EditTextの入力欄は空白、※印は消す) 108 * init() 109 */ 110 private void init() { 111 mEditText01ShopNm.setText(""); 112 mEditText01Price.setText(""); 113 114 mText01Kome01.setText(""); 115 mText01Kome04.setText(""); 116 mEditText01ShopNm.requestFocus(); // フォーカスを店名のEditTextに指定 117 } 118 119 /** 120 * ラジオボタン選択処理 121 * onCheckedChanged() 122 */ 123 @Override 124 public void onCheckedChanged(RadioGroup group, int checkedId) { 125 switch (checkedId) { 126 case R.id.radioButton01ListView: // ListView表示を選択した場合 127 intent = new Intent(MainActivity.this, SelectSheetListView.class); 128 break; 129 case R.id.radioButton01TableLayout: // TableLayout表示を選択した場合 130 intent = new Intent(MainActivity.this, SelectSheetTable.class); 131 break; 132 } 133 } 134 135 /** 136 * LocationManager#removeUpdatesメソッドで, 位置情報の更新を止める. 137 */ 138 @Override 139 protected void onPause() { 140 if(manager != null) { 141 manager.removeUpdates((LocationListener) this); 142 } 143 super.onPause(); 144 } 145 146 /** 147 LocationManager#requestLocationUpdatesメソッドで, "GPS_PROVIDER"を指定して, 位置情報の更新をリクエストする. 148 注) ネットワークからの位置情報取得の場合は, "NETWORK_PROVIDER"を指定する. 149 */ 150 @SuppressLint("MissingPermission") 151 @Override 152 protected void onResume() { 153 if(manager != null) { 154 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this); 155 } 156 super.onResume(); 157 } 158 159 /** 160 * EditTextに入力したテキストをDBに登録 161 * saveDB() 162 */ 163 private void saveList(Location location) { 164 165 // 各EditTextで入力されたテキストを取得 166 String strShopNm = mEditText01ShopNm.getText().toString(); 167 //String strLong = mEditText01Long.getText().toString(); 168 //String strLat = mEditText01Lat.getText().toString(); 169 String strPrice = mEditText01Price.getText().toString(); 170 171 // EditTextが空白の場合 172 if (strShopNm.equals("") ) { 173 mText01Kome01.setText("※"); // 店名が空白の場合、※印を表示 174 Toast.makeText(MainActivity.this, "※の箇所を入力して下さい。", Toast.LENGTH_SHORT).show(); 175 176 } else { // EditTextが全て入力されている場合 177 // 経緯度を取得する 178 double dLong = location.getLongitude(); 179 double dLat = location.getAltitude(); 180 int iPrice = Integer.parseInt(strPrice); //あとで削除 181 182 // DBへの登録処理 183 DBAdapter dbAdapter = new DBAdapter(this); 184 dbAdapter.openDB(); // DBの読み書き 185 dbAdapter.saveDB(strShopNm, dLong, dLat, iPrice); // DBに登録 186 dbAdapter.closeDB(); // DBを閉じる 187 188 init(); // 初期値設定 189 190 } 191 192 } 193 194}
補足情報(FW/ツールのバージョンなど)
AndroidStudioのバージョン:3.1.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/07 12:47
2018/10/07 15:08