Fragment と DialogFragment の間でデータを渡す最善の方法を見つけようとしています。(双方向通信)
最初に、ユーザーが旅行に使用するドライバー/車両を選択できるようにするダイアログ フラグメントがあります。ユーザーは、複数のドライバーと車両を選択できます。DialogFragment クラスのスニペットを次に示します。
public class DriverVehiclePickersDialogFragment extends DialogFragment implements OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private static final int DRIVERS_LOADER = 0;
private static final int VEHICLES_LOADER = 1;
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// These are the Contacts rows that we will retrieve.
static final String[] DRIVERS_SUMMARY_PROJECTION = new String[] { BaseColumns._ID, ConsoleContract.Drivers.DRIVER_NUMBER, ConsoleContract.Drivers.DRIVER_NAME };
static final String[] VEHICLES_SUMMARY_PROJECTION = new String[] { BaseColumns._ID, ConsoleContract.Vehicles.VEHICLE_NUMBER, ConsoleContract.Vehicles.VEHICLE_VIN };
public interface DriverVehicleDialogListener {
public void onDialogPositiveClick(????????);
}
@Override
public void onClick(View v) {
if (v.equals(mSet)) {
if (checkDriversVehiclesValidity()) {
mListener.onDialogPositiveClick(???????????);
this.dismiss();
}
} else if (v.equals(mCancel)) {
this.dismiss();
} else if (v.equals(mAddNew)) {
inflateEditRow();
} else if (v instanceof ImageButton) {
mContainerView.removeView((View) v.getParent());
}
}
// Helper for inflating a row
private void inflateEditRow() {
Bundle args = getArguments();
DialogType dialogType = (DialogType) args.getSerializable(ARG_DIALOG_TYPE);
final View rowView = getActivity().getLayoutInflater().inflate(R.layout.driver_vehicle_row, null);
CustomAutoCompleteTextView driversField, vehiclesField;
if ((dialogType == DialogType.DRIVER_SPINNER) || (dialogType == DialogType.DRIVER_VEHICLE_SPINNER)) {
driversField = (CustomAutoCompleteTextView) rowView.findViewById(R.id.drivers_field);
driversField.setThreshold(0);
driversField.setAdapter(mDriversAdapter);
driversField.setVisibility(View.VISIBLE);
}
if ((dialogType == DialogType.VEHICLE_SPINNER) || (dialogType == DialogType.DRIVER_VEHICLE_SPINNER)) {
vehiclesField = (CustomAutoCompleteTextView) rowView.findViewById(R.id.vehicles_field);
vehiclesField.setThreshold(0);
vehiclesField.setAdapter(mVehiclesAdapter);
vehiclesField.setVisibility(View.VISIBLE);
}
ImageButton mDelete = (ImageButton) rowView.findViewById(R.id.buttonDelete);
mDelete.setOnClickListener(this);
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
}
これが私の車両クラスです:
public static final class Vehicles implements VehicleColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_VEHICLES).build();
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.vehicles";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.vehicles";
private int vehicle_id, vehicle_number;
private String vehicle_vin;
public Vehicles() {
}
public Vehicles(int vehicleID, int vehicleNumber, String vehicleVin) {
setVehicleID(vehicleID);
setVehicleNumber(vehicleNumber);
setVehicleVin(vehicleVin);
}
public int getVehicleID() {
return vehicle_id;
}
public void setVehicleID(int mVehicleID) {
this.vehicle_id = mVehicleID;
}
public int getVehicleNumber() {
return vehicle_number;
}
public void setVehicleNumber(int mVehicleNumber) {
this.vehicle_number = mVehicleNumber;
}
public String getVehicleVin() {
return vehicle_vin;
}
public void setVehicleVin(String mVehicleVin) {
this.vehicle_vin = mVehicleVin;
}
public static Uri buildVehicleUri(String vehicleID) {
return CONTENT_URI.buildUpon().appendPath(vehicleID).build();
}
}
これが私のドライバークラスです:
public static final class Drivers implements DriverColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_DRIVERS).build();
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.drivers";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.drivers";
private int driver_id, driver_number;
private String driver_name;
public Drivers() {
}
public Drivers(int driverID, int driverNumber, String driverName) {
this.setDriverID(driverID);
this.setDriverNumber(driverNumber);
this.setDriverName(driverName);
}
public static Uri buildDriverUri(String driverID) {
return CONTENT_URI.buildUpon().appendPath(driverID).build();
}
public int getDriverID() {
return driver_id;
}
public void setDriverID(int driver_id) {
this.driver_id = driver_id;
}
public int getDriverNumber() {
return driver_number;
}
public void setDriverNumber(int driver_number) {
this.driver_number = driver_number;
}
public String getDriverName() {
return driver_name;
}
public void setDriverName(String driver_name) {
this.driver_name = driver_name;
}
}
そして、これがダイアログフラグメントを呼び出してドライバー/車両を選択する私のフラグメントです:
public class CrewTripFragment extends Fragment implements DateTimeDialogListener, CrewCountDialogListener, DriverVehicleDialogListener, OnItemSelectedListener {
@Override
public void onDialogPositiveClick(????) {
//I need a list of drivers+vehicles selected so i can use it in the fragment.
//an example would be:
//DriverID: 10 DriverName: John DriverNumber 30 - VehicleID 10 VehicleNumber: 24 VehicleVin: 34848
}
protected void showDriverVehicleDialog() {
// Creating a bundle object
Bundle bundleSettings = new Bundle();
bundleSettings.putInt(DriverVehiclePickersDialogFragment.ARG_TITLE_RESOURCE, R.string.crew_driver_vehicle_title);
bundleSettings.putSerializable(DriverVehiclePickersDialogFragment.ARG_DIALOG_TYPE, DriverVehiclePickersDialogFragment.DialogType.DRIVER_VEHICLE_SPINNER);
if (mDrivers != null) {
bundleSettings.putLongArray(DriverVehiclePickersDialogFragment.ARG_SET_DRIVER, mDrivers);
}
if (mVehicles != null) {
bundleSettings.putLongArray(DriverVehiclePickersDialogFragment.ARG_SET_VEHICLE, mVehicles);
}
DriverVehiclePickersDialogFragment dialogDriverVehiclePicker = DriverVehiclePickersDialogFragment.newInstance(this, bundleSettings);
dialogDriverVehiclePicker.show(getFragmentManager(), getTag());
}
ダイアログフラグメントからドライバーのリストと車両のリストをフラグメントに戻す必要があると考えていましたか?
URI の配列を Fragment に戻すことも考えていましたが、データベースが Web サービスと同期したままであり、同期して sqlite データベース内のレコードが削除された場合、それはできません。フラグメント内のデータを取得します。
何が最善の解決策なのか本当にわかりません。どんな助けでも大歓迎です。