Passing dialog selected items to other activities or adapters











up vote
2
down vote

favorite
1












I am have struggling with trying to add a feature on my project and I need some help on how I can move beyond this step. So I have decided to create a method selectCurrency() on my preferencesFragment class As you can see I have created a simple Dialog with not more than 6 currencies, what I want to do is once a currency is selected from this dialog I want to display it on my currency adapter.



public class PreferencesFragment extends PreferenceFragment {

final static String items = {"$ - US Dollar", "€ - Euro", "£ - British Pound","IRN - IRN ","A$ - Australian Dollar", " CA$ - Canadian Dollar"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.pref_general);

//show currency
final Preference currencyPreference = findPreference
(getResources().getString(R.string.setting_category_currency_change_button_key));
currencyPreference.setOnPreferenceClickListener(preference -> {

selectCurrency();

return false;
});

}



@RequiresApi(api = Build.VERSION_CODES.M)
public void selectCurrency() {


AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select Currency")

.setItems(items, (dialog, which) -> {

// String selectedText = items[which].toString();
Toast.makeText(getActivity(), items[which] + " was selected", Toast.LENGTH_SHORT).show();
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = builder.create();

alertDialog.show();

Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setBackgroundColor(Color.argb(100,100,25,51));
button.setTextColor(Color.WHITE);
}

}


Here is my adapter.
So currently I am using a string which is not right, so how can I get the selected currency from the above fragment to my adapter down here: at holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());



public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.GeneralViewHolder> {

@Override
public void onBindViewHolder(GeneralViewHolder holder, int position) {
if (getItemViewType(position) == -1) {
MonthSummaryCard holder1 = (MonthSummaryCard) holder;
holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());
holder1.setTotalExpensesPerMonth("$ " + Double.valueOf(totalExpenseAmount).toString());









share|improve this question
























  • @Benoit any help?
    – Anna Murray
    Nov 5 at 21:07










  • Have you tried using an interface then notify the adapter when the data changes?
    – jahson kimulu
    Nov 6 at 8:03















up vote
2
down vote

favorite
1












I am have struggling with trying to add a feature on my project and I need some help on how I can move beyond this step. So I have decided to create a method selectCurrency() on my preferencesFragment class As you can see I have created a simple Dialog with not more than 6 currencies, what I want to do is once a currency is selected from this dialog I want to display it on my currency adapter.



public class PreferencesFragment extends PreferenceFragment {

final static String items = {"$ - US Dollar", "€ - Euro", "£ - British Pound","IRN - IRN ","A$ - Australian Dollar", " CA$ - Canadian Dollar"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.pref_general);

//show currency
final Preference currencyPreference = findPreference
(getResources().getString(R.string.setting_category_currency_change_button_key));
currencyPreference.setOnPreferenceClickListener(preference -> {

selectCurrency();

return false;
});

}



@RequiresApi(api = Build.VERSION_CODES.M)
public void selectCurrency() {


AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select Currency")

.setItems(items, (dialog, which) -> {

// String selectedText = items[which].toString();
Toast.makeText(getActivity(), items[which] + " was selected", Toast.LENGTH_SHORT).show();
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = builder.create();

alertDialog.show();

Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setBackgroundColor(Color.argb(100,100,25,51));
button.setTextColor(Color.WHITE);
}

}


Here is my adapter.
So currently I am using a string which is not right, so how can I get the selected currency from the above fragment to my adapter down here: at holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());



public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.GeneralViewHolder> {

@Override
public void onBindViewHolder(GeneralViewHolder holder, int position) {
if (getItemViewType(position) == -1) {
MonthSummaryCard holder1 = (MonthSummaryCard) holder;
holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());
holder1.setTotalExpensesPerMonth("$ " + Double.valueOf(totalExpenseAmount).toString());









share|improve this question
























  • @Benoit any help?
    – Anna Murray
    Nov 5 at 21:07










  • Have you tried using an interface then notify the adapter when the data changes?
    – jahson kimulu
    Nov 6 at 8:03













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I am have struggling with trying to add a feature on my project and I need some help on how I can move beyond this step. So I have decided to create a method selectCurrency() on my preferencesFragment class As you can see I have created a simple Dialog with not more than 6 currencies, what I want to do is once a currency is selected from this dialog I want to display it on my currency adapter.



public class PreferencesFragment extends PreferenceFragment {

final static String items = {"$ - US Dollar", "€ - Euro", "£ - British Pound","IRN - IRN ","A$ - Australian Dollar", " CA$ - Canadian Dollar"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.pref_general);

//show currency
final Preference currencyPreference = findPreference
(getResources().getString(R.string.setting_category_currency_change_button_key));
currencyPreference.setOnPreferenceClickListener(preference -> {

selectCurrency();

return false;
});

}



@RequiresApi(api = Build.VERSION_CODES.M)
public void selectCurrency() {


AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select Currency")

.setItems(items, (dialog, which) -> {

// String selectedText = items[which].toString();
Toast.makeText(getActivity(), items[which] + " was selected", Toast.LENGTH_SHORT).show();
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = builder.create();

alertDialog.show();

Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setBackgroundColor(Color.argb(100,100,25,51));
button.setTextColor(Color.WHITE);
}

}


Here is my adapter.
So currently I am using a string which is not right, so how can I get the selected currency from the above fragment to my adapter down here: at holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());



public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.GeneralViewHolder> {

@Override
public void onBindViewHolder(GeneralViewHolder holder, int position) {
if (getItemViewType(position) == -1) {
MonthSummaryCard holder1 = (MonthSummaryCard) holder;
holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());
holder1.setTotalExpensesPerMonth("$ " + Double.valueOf(totalExpenseAmount).toString());









share|improve this question















I am have struggling with trying to add a feature on my project and I need some help on how I can move beyond this step. So I have decided to create a method selectCurrency() on my preferencesFragment class As you can see I have created a simple Dialog with not more than 6 currencies, what I want to do is once a currency is selected from this dialog I want to display it on my currency adapter.



public class PreferencesFragment extends PreferenceFragment {

final static String items = {"$ - US Dollar", "€ - Euro", "£ - British Pound","IRN - IRN ","A$ - Australian Dollar", " CA$ - Canadian Dollar"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from the XML resource
addPreferencesFromResource(R.xml.pref_general);

//show currency
final Preference currencyPreference = findPreference
(getResources().getString(R.string.setting_category_currency_change_button_key));
currencyPreference.setOnPreferenceClickListener(preference -> {

selectCurrency();

return false;
});

}



@RequiresApi(api = Build.VERSION_CODES.M)
public void selectCurrency() {


AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select Currency")

.setItems(items, (dialog, which) -> {

// String selectedText = items[which].toString();
Toast.makeText(getActivity(), items[which] + " was selected", Toast.LENGTH_SHORT).show();
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = builder.create();

alertDialog.show();

Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setBackgroundColor(Color.argb(100,100,25,51));
button.setTextColor(Color.WHITE);
}

}


Here is my adapter.
So currently I am using a string which is not right, so how can I get the selected currency from the above fragment to my adapter down here: at holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());



public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.GeneralViewHolder> {

@Override
public void onBindViewHolder(GeneralViewHolder holder, int position) {
if (getItemViewType(position) == -1) {
MonthSummaryCard holder1 = (MonthSummaryCard) holder;
holder1.setWalletAmountView("$ " + Double.valueOf(walletBalance).toString());
holder1.setTotalExpensesPerMonth("$ " + Double.valueOf(totalExpenseAmount).toString());






java android dialog alertdialog android-alertdialog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 21:03

























asked Nov 5 at 17:12









Anna Murray

326




326












  • @Benoit any help?
    – Anna Murray
    Nov 5 at 21:07










  • Have you tried using an interface then notify the adapter when the data changes?
    – jahson kimulu
    Nov 6 at 8:03


















  • @Benoit any help?
    – Anna Murray
    Nov 5 at 21:07










  • Have you tried using an interface then notify the adapter when the data changes?
    – jahson kimulu
    Nov 6 at 8:03
















@Benoit any help?
– Anna Murray
Nov 5 at 21:07




@Benoit any help?
– Anna Murray
Nov 5 at 21:07












Have you tried using an interface then notify the adapter when the data changes?
– jahson kimulu
Nov 6 at 8:03




Have you tried using an interface then notify the adapter when the data changes?
– jahson kimulu
Nov 6 at 8:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159067%2fpassing-dialog-selected-items-to-other-activities-or-adapters%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159067%2fpassing-dialog-selected-items-to-other-activities-or-adapters%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()