【发布时间】:2016-02-10 14:25:00
【问题描述】:
我在名为 onConnected 的方法中从 Activity 获取数据,我获取当前位置:经度、纬度。如何将这些数据放入片段?是的,我知道 Bundle 以及如何通过这种方式传递数据,但我无法创建 Bundle在 onConnected 方法和 setArguments 中,因为我在 onCreate Activity 方法中创建了片段。我创建了类变量经度和纬度,但在 onCreate 中,变量 = 0。如何将数据发送到片段?我需要在 Activity 中获取当前位置和将其发送到片段。
活动:
public class ScreenForTagging extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
double latitude,longitude;
Location mLastLocation;
GoogleApiClient mGoogleApiClient;
Bundle bundlelocation;
Tagging_screen tagging_screen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_for_tagging);
tagging_screen = new Tagging_screen();
bundlelocation = new Bundle();
bundlelocation.putDouble("latitude",latitude);
bundlelocation.putDouble("longitude", longitude);
tagging_screen.setArguments(bundlelocation);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentCont, tagging_screen);
fragmentTransaction.commit();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longitude = mLastLocation.getLongitude();
Toast.makeText(ScreenForTagging.this, "latitude:"+latitude+"longitude"+longitude, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void onStop(){
super.onStop();
mGoogleApiClient.disconnect();
}
}
片段:
class Tagging_screen extends Fragment {
Location mLastLocation;
String URL;
Button btndownload;
ImageView imageView;
EditText editText;
Button btngallery;
FloatingActionButton floatbtn;
LocationManager locationManager;
GoogleApiClient mGoogleApiClient;
double latitude, longitude;
private static final int RESULT_LOAD_IMAGE = 1;
Context mContext;
// private static final int RESULT_OK = 2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tagging_screen, null);
imageView = (ImageView) v.findViewById(R.id.imageView);
editText = (EditText) v.findViewById(R.id.editText_download);
btndownload = (Button) v.findViewById(R.id.btn_download);
btngallery = (Button) v.findViewById(R.id.button_gallery);
floatbtn = (FloatingActionButton) v.findViewById(R.id.floatbutton);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
mContext = getActivity();
return v;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// latitude = getArguments().getDouble("latitude");
// longitude = getArguments().getDouble("longitude");
floatbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btndownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
URL = editText.getText().toString();
Picasso.with(getActivity()).load(URL).into(imageView);
}
});
btngallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.d("ActivityResult", "OnActivityResult");
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
【问题讨论】:
-
您尝试过使用 SharedPreferences 吗?
-
使用 FindFragmentById/Tag 并调用片段内部的方法。更多这里stackoverflow.com/questions/8532462/…
标签: android android-fragments android-activity