1

因此,我爲Google地圖的應用程序提供了地圖片段。我想將標記(lat/lng)中的值傳遞給保存位置的String,然後我想將該值傳遞給另一個應用程序中的TextField。該文本字段將被存儲到我已經構建的SQLite數據庫中。如何將片段按鈕的值傳遞給另一個活動

我的當前地圖的活動是如下

public class MapActivity extends FragmentActivity 
    implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener, 
    Serializable { 

GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private Location mCurrentLocation; 
private static final int ERROR_DIALOG_REQUEST = 9001; 
private static final int EDITOR_REQUEST_CODE = 1001; 
public static final String LOCAT_KEY = "location"; 
private GoogleApiClient mLocationClient; 
private Marker marker; 
Bundle bundle; 
String value; 
private static final double 
     CITY_LAT = 53.3478, 
     CITY_LNG = -6.2597; 

Circle shape; 
public String lat; 
public String lng; 
public String location; 

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

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
    boolean enabled = service 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 

    // check if enabled and if not send user to the GSP settings 

    if (!enabled) { 
     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(intent); 
    } 

    // Getting reference to Button 
    Button btnDraw = (Button) findViewById(R.id.btn_draw); 

    if (servicesOK()) { 
     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     if (initMap()) { 

      gotoLocation(CITY_LAT, CITY_LNG, 12); 

      mMap.setMyLocationEnabled(true); 

      mLocationClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 

      mLocationClient.connect(); 


     } else { 
      Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show(); 
     } 

    } else { 
     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    } 

    bundle = new Bundle(); 

    btnDraw.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      String location = lat + "," + lng; 
      // Checks, whether location is captured 
      ((TextView) findViewById(R.id.editLocation)).setText(location); 
     } 
    }); 
} 

注意,我只添加代碼到實際的按鈕時啓動。

我爲被點擊我的XML文件中的按鈕代碼如下:

<Button 
android:id="@+id/btn_draw" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/save_location_btn" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:onClick="mapLocationClick"/> 

最後我對編輯器類代碼如下:

public class EditorActivity extends AppCompatActivity { 

public static final String KEY_ID = "id"; 
public static final String KEY_TIME = "time" ; 
public static final String KEY_LOCAT = "location"; 


private String action; 
private EditText editor; 
private EditText editorDate; 
private EditText editorTime; 
private EditText editorLocation; 
private ImageButton dateButton; 
private ImageButton timeButton; 
private ImageButton locationButton; 
private String noteFilter; 
private String oldText; 
private String oldDate; 
private String oldTime; 
private String oldLocation; 
String value = null; 


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

    editor = (EditText) findViewById(R.id.editText); 
    editorDate = (EditText) findViewById(R.id.editDate); 
    editorTime = (EditText) findViewById(R.id.editTime); 
    editorLocation = (EditText) findViewById(R.id.editLocation); 
    dateButton = (ImageButton) findViewById(R.id.imgButtonCal); 
    timeButton = (ImageButton) findViewById(R.id.imgButtonClock); 
    locationButton = (ImageButton) findViewById(R.id.imgButtonMap); 

    //enableEdit = (FloatingActionButton) findViewById(R.id.fabEdit); 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras == null) { 
     action = Intent.ACTION_INSERT; 
     setTitle(getString(R.string.new_note)); 
    } 
    else { 
     long id = extras.getLong(KEY_ID); 

     if (id == 0){ 
      action = Intent.ACTION_INSERT; 
      setTitle(getString(R.string.new_note)); 

      long time = intent.getLongExtra(KEY_TIME, 0); 
      if (time != 0) { 
       Date d = new Date(time); 
       String dateString= DateFormat.format("yyyy-MM-dd", `d).toString();` 
       editorDate.setText(dateString); 
      } 

     } 

     else { 
      action = Intent.ACTION_EDIT; 
      setTitle(getString(R.string.edit_note)); 

      Uri uri = Uri.parse(NotesProvider.CONTENT_URI + "/" + id); 
      noteFilter = DBOpenHelper.NOTE_ID + "=" + uri.getLastPathSegment(); 

      Cursor cursor; 
      cursor = getContentResolver().query(uri, 
        DBOpenHelper.ALL_COLUMNS, noteFilter, null, null); 
      cursor.moveToFirst(); 
      oldText = cursor.getString(cursor.getColumnIndex(NOTE_TEXT)); 
      oldDate = cursor.getString(cursor.getColumnIndex(NOTE_DATE)); 
      oldTime = cursor.getString(cursor.getColumnIndex(NOTE_TIME)); 
      oldLocation = cursor.getString(cursor.getColumnIndex(NOTE_LOCATION)); 
      editor.setText(oldText); 
      editor.setEnabled(false); 
      editorDate.setText(oldDate); 
      editorDate.setEnabled(false); 
      dateButton.setEnabled(false); 
      editorTime.setText(oldTime); 
      editorTime.setEnabled(false); 
      timeButton.setEnabled(false); 
      editorLocation.setText(oldLocation); 
      editorLocation.setEnabled(false); 
      locationButton.setEnabled(false); 
      //saveButton.setEnabled(false); 
      editor.requestFocus(); 
      //enableEdit.setEnabled(true); 
      //enableSave.setEnabled(false); 
     } 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (action.equals(Intent.ACTION_EDIT)){ 
     getMenuInflater().inflate(R.menu.menu_editor, menu); 
    } 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    switch (item.getItemId()) { 
     case android.R.id.home: 
      finishEditing(); 
      break; 
     case R.id.action_delete: 
      deleteNote(); 
      break; 
     case R.id.action_edit: 
      enableFields(); 
      break; 
    } 

    return true; 
} 
private void enableFields(){ 
    if(NotesProvider.CONTENT_URI != null) { 
     editor.setEnabled(true); 
     editorDate.setEnabled(true); 
     dateButton.setEnabled(true); 
     editorTime.setEnabled(true); 
     timeButton.setEnabled(true); 
     editorLocation.setEnabled(true); 
     locationButton.setEnabled(true); 

    } 
} 
private void deleteNote() { 
    getContentResolver().delete(NotesProvider.CONTENT_URI, 
      noteFilter,null); 
    Toast.makeText(this, R.string.note_deleted, 
      Toast.LENGTH_SHORT).show(); 
    setResult(RESULT_OK); 
    finish(); 
} 

private void finishEditing(){ 
    String newText = editor.getText().toString().trim(); 
    String newDate = editorDate.getText().toString().trim(); 
    String newTime = editorTime.getText().toString().trim(); 
    String newLocation = editorLocation.getText().toString().trim(); 

    switch (action) { 
     case Intent.ACTION_INSERT: 
      if (newText.length() == 0 && newDate.length() == 0 && newTime.length() == 0){ 
       setResult(RESULT_CANCELED); 
      } else{ 
       insertNote(newText, newDate, newTime, newLocation); 
      } 
      break; 
     case Intent.ACTION_EDIT: 
      if (newText.length() == 0 && newDate.length() == 0 && newTime.length() == 0 && newLocation.length() == 0){ 
       deleteNote(); 
      }else if (oldText.equals(newText) && oldDate.equals(newDate) && oldTime.equals(newTime) && oldLocation.equals(newLocation)){ 
       setResult(RESULT_CANCELED); 
      }else { 
       updateNote(newText, newDate, newTime, newLocation); 
      } 
    } 
    finish(); 
} 

private void updateNote(String noteText, String noteDate, String noteTime, String noteLocation) { 
    ContentValues values = new ContentValues(); 
    values.put(NOTE_TEXT, noteText); 
    values.put(NOTE_DATE, noteDate); 
    values.put(NOTE_TIME, noteTime); 
    values.put(NOTE_LOCATION, noteLocation); 
    getContentResolver().update(NotesProvider.CONTENT_URI, values, noteFilter, null); 
    Toast.makeText(this, R.string.note_updated, Toast.LENGTH_SHORT).show(); 
    setResult(RESULT_OK); 
} 

private void insertNote(String noteText, String noteDate, String noteTime, String noteLocation) { 
    ContentValues values = new ContentValues(); 
    values.put(NOTE_TEXT, noteText); 
    values.put(NOTE_DATE, noteDate); 
    values.put(NOTE_TIME, noteTime); 
    values.put(NOTE_LOCATION, noteLocation); 
    getContentResolver().insert(NotesProvider.CONTENT_URI, values); 
    setResult(RESULT_OK); 
} 

@Override 
public void onBackPressed() { 
    finishEditing(); 
} 

public void onSaveNote(View view) { finishEditing();} 

public void onButtonClicked(View v){ 
    TimePickerFragment newFragment = new TimePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "timePicker"); 
} 

public void showDatePickerDialog(View v) { 
    DatePickerFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
} 

public void openMapFragment(View v) { 
    Intent intent = new Intent(this, MapActivity.class); 
    startActivity(intent); 
} 

人幫助我,所以當我點擊按鈕時,它會從該位置獲取值並將其保存在之前加載的上一個類中。

回答

0

我找到了答案。我創建了我的地圖類

btnDraw.setOnClickListener以下(新View.OnClickListener(){

 @Override 
     public void onClick(View v) { 
      // Checks, whether location is captured 
      Intent intent = MapActivity.this.getIntent(); 
      intent.putExtra(LATITUDE_EXTRA, lat); 
      intent.putExtra(LONGITUDE_EXTRA, lng); 
      MapActivity.this.setResult(RESULT_OK, intent); 
      MapActivity.this.finish(); 
     } 
    }); 

然後由下面我把它添加到編輯器類。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == MAP_REQUEST_CODE && resultCode == RESULT_OK) { 
     String lat = data.getStringExtra(MapActivity.LATITUDE_EXTRA); 
     String lng = data.getStringExtra(MapActivity.LONGITUDE_EXTRA); 

     editorLocation.setText(lat + ", " + lng); 
    } 
    else { 
     Toast.makeText(this, "Error!", Toast.LENGTH_LONG).show(); 
    } 
} 

似乎我需要添加一個新的意圖,並使用onActivityResult。

相關問題