在警告对话框中,我想显示所选的值

时间:2016-02-02 07:30:02

标签: java android

public class QuotesFragment extends Fragment implements QuotesDataHelper.QuotesListener, QuotesAdapter.OnQuoteActionListener {

public static final String TAG = QuotesFragment.class.getSimpleName();

private Views mViews;
private QuotesDataHelper mDataHelper;
private Quote mSelectedQuote;

public QuotesFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment
 *
 * @return A new instance of fragment QuotesFragment.
 */
public static QuotesFragment newInstance() {
    QuotesFragment fragment = new QuotesFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
}

/**
 * Retrieve the request for quotes data from service
 */
private void getQuotesData() {
    showProgress(true);
    mDataHelper.getQuotesDetails(LobbSharedPreferences.getFromSharedPreferences(getContext(), StringUtils.LOBB_SHARED_PREFERENCE_TRUCK_OWNER_ID));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_quotes_page, container, false);
    mViews = new Views(rootView);
    mDataHelper = new QuotesDataHelper(this);
    getQuotesData();
    return rootView;
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}

/**
 * Shows the progress UI and hides the dashboard.
 */
private void showProgress(final boolean show) {
    mViews.mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
    mViews.mQuotesView.setVisibility(show ? View.GONE : View.VISIBLE);
}

private void showNoDataView(String message) {
    mViews.mNoDataView.setVisibility(View.VISIBLE);
    mViews.mQuotesView.setVisibility(View.GONE);
    mViews.mNoDataView.setText(message);
}

private void showToast(String message) {
    Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}

@Override
public void showQuotes(ArrayList<Quote> quotesList) {
    showProgress(false);
    if (quotesList != null && quotesList.size() > 0) {
        mViews.mQuotesView.setHasFixedSize(true);
        mViews.mQuotesView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mViews.mQuotesView.setLayoutManager(layoutManager);
        QuotesAdapter mAdapter = new QuotesAdapter(getContext(), quotesList);
        mAdapter.setQuoteActionListener(this);
        mViews.mQuotesView.setAdapter(mAdapter);
    } else {
        showNoDataView(getResources().getString(R.string.quotes_nodata));
    }
}



@Override
public void showError(String title, String errorMessage) {
    showProgress(false);
    showNoDataView(errorMessage);
}

@Override
public void onQuoteIgnored() {
    showProgress(false);
    showToast(getResources().getString(R.string.quote_ignore_success));
    //Quote ignored . So reload the data
    getQuotesData();
}

@Override
public void onQuoteIgnoreFailed() {
    showProgress(false);
    if (!((Activity) getContext()).isFinishing()) {
        new android.app.AlertDialog.Builder(getContext()).setTitle(getResources().getString(R.string.error_title)).setMessage(getResources().getString(R.string.error_ignore_quote)).setNeutralButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}

@Override
public void onCreateQuoteSuccess() {
    showProgress(false);
    showToast(getResources().getString(R.string.quote_create_success));
    //Quote created. So refresh
    getQuotesData();
}

@Override
public void onCreateQuoteFailure() {
    showProgress(false);
    showToast(getResources().getString(R.string.quote_create_failure));
}

@Override
public void onQuote(Quote quote) {
    mSelectedQuote = quote;
    showConfirmationAlert();
}
private void showConfirmationAlert() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage(getResources().getString(R.string.quote_confirm));
    builder.setCancelable(true);

    builder.setPositiveButton(
            getResources().getString(R.string.quote_confirm_yes),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    showProgress(true);
                    mDataHelper.createQuote(mSelectedQuote);
                    dialog.cancel();
                }
            });

    builder.setNegativeButton(
            getResources().getString(R.string.quote_confirm_no),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog confirmationAlert = builder.create();
    confirmationAlert.show();
}

@Override
public void onIgnore(int requestID) {
    showProgress(true);
    mDataHelper.ignoreQuote(requestID);
}

private class Views {
    RecyclerView mQuotesView;
    View mProgressView;
    TextView mNoDataView;

    Views(View root) {
        mQuotesView = (RecyclerView) root.findViewById(R.id.view_myquotes);
        mProgressView = root.findViewById(R.id.progress_container_quotes);
        mNoDataView = (TextView) root.findViewById(R.id.view_quotes_nodata);
    }
}

}

[Screenshot "Are you sure you want to Quote AMOUNT  "]

private void showConfirmationAlert() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage(getResources().getString(R.string.quote_confirm));
    builder.setCancelable(true);

    builder.setPositiveButton(
            getResources().getString(R.string.quote_confirm_yes),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    showProgress(true);
                    mDataHelper.createQuote(mSelectedQuote);
                    dialog.cancel();
                }
            });

    builder.setNegativeButton(
            getResources().getString(R.string.quote_confirm_no),
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog confirmationAlert = builder.create();
    confirmationAlert.show();
}

1 个答案:

答案 0 :(得分:1)

  • 您在mSelectedQuote mth。
  • 中获得了onQuote()
  • 现在您可以在mSelectedQuote
  • 上获得金额
  • 将其传递给showConfirmationAlert(金额);
  • 将其设置为AlertDialog,如下所示 -

    private void showConfirmationAlert(int amount) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage(getResources().getString(R.string.quote_confirm) + amount);