2015-12-10 63 views
1

我正在開發一個項目,允許用戶從MySQL數據庫添加/更新/刪除記錄。該應用程序成功上傳並從數據庫中檢索所有信息。我試圖獲得lat和long來顯示自己,只要我發送來自仿真器控制的值而不是用戶必須通過文本字段手動輸入值,這是我最初編程它的方式。顯示後,值應與其他文本字段值一起插入到數據庫中。我希望這是有道理的..現在,當我從模擬器控件發送值他們不顯示在textView中,我不知道爲什麼?任何幫助將不勝感激。Android開發顯示併發送經緯度到數據庫

更新:這是哪裏拉和長視圖正在初始化,應該從模擬器控制獲取值?

//Defining views 
//private EditText editTextLatitude; 
//private EditText editTextLongitude; 
private EditText editTextTimeInserted; 
private EditText editTextUserID; 
private TextView latituteField; 
private TextView longitudeField; 
private LocationManager locationManager; 
private String provider; 

private Button buttonAdd; 
private Button buttonView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Initializing views 
    latituteField = (TextView) findViewById(R.id.TextView02); 
    longitudeField = (TextView) findViewById(R.id.TextView04); 
    //editTextLatitude = (EditText) findViewById(R.id.editTextLat); 
    //editTextLongitude = (EditText) findViewById(R.id.editTextLon); 
    editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted); 
    editTextUserID = (EditText) findViewById(R.id.editTextUserID); 

    buttonAdd = (Button) findViewById(R.id.buttonAdd); 
    buttonView = (Button) findViewById(R.id.buttonView); 

    //Setting listeners to button 
    buttonAdd.setOnClickListener(this); 
    buttonView.setOnClickListener(this); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the location provider -> use 
    // default 
    Criteria criteria = new Criteria(); 
    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    // Initialize the location fields 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     onLocationChanged(location); 
    } else { 
     latituteField.setText("Location not available"); 
     longitudeField.setText("Location not available"); 
    } 
} 

public void onLocationChanged(final Location location) { 
    int lat = (int) (location.getLatitude()); 
    int lng = (int) (location.getLongitude()); 
    latituteField.setText(String.valueOf(lat)); 
    longitudeField.setText(String.valueOf(lng)); 

    } 
+0

以外的任何其他細節?你能縮小範圍並告訴你有什麼?它來到客戶端嗎?沒有人想要學習你的完整代碼 –

+0

當我運行應用程序時顯示textViews,但是當我從模擬器發送lat和long的值時,它們不顯示它們,留意上面的更新,我會縮小範圍這個@IliiazAkhmedov的代碼,我也很新,所以如果我沒有馬上得到任何東西,請耐心等待。 – Tuks

回答

1

中添加的manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

// Create an instance of GoogleAPIClient. 

if (mGoogleApiClient == null) { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
    .addConnectionCallbacks(this) 
    .addOnConnectionFailedListener(this) 
    .addApi(LocationServices.API) 
    .build(); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
     mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
    } 
} 

follow the link

+0

你爲什麼使用'FusedLocationApi'? –

+0

簡單的API,功率效率,立即可用.... –

相關問題