如何在共享流程中处理矢量资产? (Android/Java)

发布于 2025-02-06 05:16:23 字数 10060 浏览 0 评论 0原文

我一直在开发一个Android应用程序,该应用程序允许用户跟踪预算支出记录。由于这是预算支出跟踪器,因此我希望用户选择其货币。在这种情况下,我有三种货币:美元,日元和欧元。 为了启用它,我创建了三个类:BudgingTrackerStingSactivity,Currencyspinneradapter和Currency。 默认货币为美元。我希望它在EditText的开始时显示所选的货币符号,例如$,¥和€,是矢量资产。

由于矢量资产本身无法通过共享流程来处理,因此我使用IF-ELSE语句将执行分支以在EditText中显示货币符号。

问题在于它暂时更改货币符号。但是,当我重新启动应用程序时,更改可以追溯到默认设置,并且货币始终是美元。除非用户再次更改它,否则如何使其永久保留在设置中?

budgeTTrackerSettingSactivity.java

package com.myproject.offlinebudgettrackerappproject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.myproject.offlinebudgettrackerappproject.adapter.CurrencySpinnerAdapter;

public class BudgetTrackerSettingsActivity extends AppCompatActivity {

    Spinner spinner;
    public static String spinnerText;

    Button currencyBtn;
    String selectedCurrency;
    String finalCurrency;
    private static final String PREF_CURRENCY_VALUE = "currencyValue";

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

        spinner = (Spinner) findViewById(R.id.currency_spinner);
        currencyBtn = (Button) findViewById(R.id.currency_btn);

        CurrencySpinnerAdapter currencySpinnerAdapter = new CurrencySpinnerAdapter(this, R.layout.currency_spinner_adopter,
                Currency.getCurrencyArrayList());
        spinner.setAdapter(currencySpinnerAdapter);

        final SharedPreferences sharedPreferences = getSharedPreferences("CURRENCY_SHARED", 0);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Currency currency = (Currency) spinner.getSelectedItem();
                spinnerText = currency.getCurrency();
                if (spinnerText == "US Dollars") {
                    Toast.makeText(BudgetTrackerSettingsActivity.this, "US Dollars", Toast.LENGTH_SHORT).show();
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "US_DOLLAR";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else if (spinnerText == "Japanese Yen") {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "JAPANESE_YEN";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "EURO";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.putInt(PREF_CURRENCY_VALUE, 2);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                Log.d("Currency", "onCreate: " + spinnerText);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                spinnerText = null;
            }
        });
    }


}

currencyspinneradapter.java

package com.myproject.offlinebudgettrackerappproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.myproject.offlinebudgettrackerappproject.Currency;
import com.myproject.offlinebudgettrackerappproject.R;

import java.util.List;

public class CurrencySpinnerAdapter extends ArrayAdapter<Currency> {

    LayoutInflater layoutInflater;

    public CurrencySpinnerAdapter(@NonNull Context context, int resource, @NonNull List<Currency> currencies) {
        super(context, resource, currencies);
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView = layoutInflater.inflate(R.layout.currency_spinner_adopter, null, true);
        Currency currency = getItem(position);
        TextView textView = (TextView) rowView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());
        return rowView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.currency_spinner_adopter, parent, false);
        }

        Currency currency = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());

        return convertView;
    }
}

Currency.java

package com.myproject.offlinebudgettrackerappproject;

import java.util.ArrayList;

public class Currency {

    private static ArrayList<Currency> currencyArrayList = new ArrayList<>();

    private String id;
    private String currency;

    public Currency(String id, String currency) {
        this.id = id;
        this.currency = currency;
    }

    public Currency() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public static void initCurrencies() {
        Currency usd = new Currency("0", "US Dollars");
        currencyArrayList.add(usd);

        Currency jpy = new Currency("1", "Japanese Yen");
        currencyArrayList.add(jpy);

        Currency euro = new Currency("2", "Euro");
        currencyArrayList.add(euro);


    }

    public int getCurrencyImage() {
        switch (getId()) {
            case "0":
                return R.drawable.ic_money;
            case "1":
                return R.drawable.ic_yen;
            case "2":
                return R.drawable.ic_euro;
        }
        return R.drawable.ic_money;
    }

    public static ArrayList<Currency> getCurrencyArrayList() {
        return currencyArrayList;
    }
}

homefragment.java(这是接收更改设置的类)

final SharedPreferences sharedPreferences = getActivity().getSharedPreferences("CURRENCY_SHARED", 0);;

        //if-else branch to select currency
        String currentCurrency = sharedPreferences.getString("CURRENCY", "");

        currentCurrencyTv.setText(currentCurrency);
        if (currentCurrency == "US Dollars") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_money, 0, 0, 0);
        } else if (currentCurrency == "Japanese Yen") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_yen, 0, 0, 0);
        } else if (currentCurrency == "Euro") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_euro, 0, 0, 0);
        }

I've been developing an Android app that allows users to track budget spending records. Since this is a budget spending tracker, I want users to select their currencies. In this case, I have three currencies: US Dollar, Japanese Yen, and Euro.
To enable it, I created three classes: BudgetTrackerSettingsActivity, CurrencySpinnerAdapter, and Currency.
The default currency is US Dollar. And I want it to display the selected currency symbols, such as $,¥, and €, which are Vector Assets, at the beggining of EditText.

Since Vector Assets themselves can't be handled by SharedPreferences, I used an if-else statement to branch the execution to display the currency symbols in EditText.

The problem is that it temporarily changes the currency symbol. But when I restart the app, the change goes back to the default settings and the currency is always US Dollar. How can I make it remain in the settings permanently unless the user changes it again?

BudgetTrackerSettingsActivity.java

package com.myproject.offlinebudgettrackerappproject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.myproject.offlinebudgettrackerappproject.adapter.CurrencySpinnerAdapter;

public class BudgetTrackerSettingsActivity extends AppCompatActivity {

    Spinner spinner;
    public static String spinnerText;

    Button currencyBtn;
    String selectedCurrency;
    String finalCurrency;
    private static final String PREF_CURRENCY_VALUE = "currencyValue";

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

        spinner = (Spinner) findViewById(R.id.currency_spinner);
        currencyBtn = (Button) findViewById(R.id.currency_btn);

        CurrencySpinnerAdapter currencySpinnerAdapter = new CurrencySpinnerAdapter(this, R.layout.currency_spinner_adopter,
                Currency.getCurrencyArrayList());
        spinner.setAdapter(currencySpinnerAdapter);

        final SharedPreferences sharedPreferences = getSharedPreferences("CURRENCY_SHARED", 0);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Currency currency = (Currency) spinner.getSelectedItem();
                spinnerText = currency.getCurrency();
                if (spinnerText == "US Dollars") {
                    Toast.makeText(BudgetTrackerSettingsActivity.this, "US Dollars", Toast.LENGTH_SHORT).show();
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "US_DOLLAR";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else if (spinnerText == "Japanese Yen") {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "JAPANESE_YEN";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                } else {
                    currencyBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            selectedCurrency = spinnerText;
                            finalCurrency = "EURO";

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("CURRENCY", selectedCurrency);
                            editor.putString(PREF_CURRENCY_VALUE, finalCurrency);
                            editor.putInt(PREF_CURRENCY_VALUE, 2);
                            editor.commit();
                            Toast.makeText(BudgetTrackerSettingsActivity.this, "Currency Saved", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(BudgetTrackerSettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                Log.d("Currency", "onCreate: " + spinnerText);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                spinnerText = null;
            }
        });
    }


}

CurrencySpinnerAdapter.java

package com.myproject.offlinebudgettrackerappproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.myproject.offlinebudgettrackerappproject.Currency;
import com.myproject.offlinebudgettrackerappproject.R;

import java.util.List;

public class CurrencySpinnerAdapter extends ArrayAdapter<Currency> {

    LayoutInflater layoutInflater;

    public CurrencySpinnerAdapter(@NonNull Context context, int resource, @NonNull List<Currency> currencies) {
        super(context, resource, currencies);
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView = layoutInflater.inflate(R.layout.currency_spinner_adopter, null, true);
        Currency currency = getItem(position);
        TextView textView = (TextView) rowView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());
        return rowView;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.currency_spinner_adopter, parent, false);
        }

        Currency currency = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.currency_name_textview);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.currency_symbol);
        textView.setText(currency.getCurrency());
        imageView.setImageResource(currency.getCurrencyImage());

        return convertView;
    }
}

Currency.java

package com.myproject.offlinebudgettrackerappproject;

import java.util.ArrayList;

public class Currency {

    private static ArrayList<Currency> currencyArrayList = new ArrayList<>();

    private String id;
    private String currency;

    public Currency(String id, String currency) {
        this.id = id;
        this.currency = currency;
    }

    public Currency() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public static void initCurrencies() {
        Currency usd = new Currency("0", "US Dollars");
        currencyArrayList.add(usd);

        Currency jpy = new Currency("1", "Japanese Yen");
        currencyArrayList.add(jpy);

        Currency euro = new Currency("2", "Euro");
        currencyArrayList.add(euro);


    }

    public int getCurrencyImage() {
        switch (getId()) {
            case "0":
                return R.drawable.ic_money;
            case "1":
                return R.drawable.ic_yen;
            case "2":
                return R.drawable.ic_euro;
        }
        return R.drawable.ic_money;
    }

    public static ArrayList<Currency> getCurrencyArrayList() {
        return currencyArrayList;
    }
}

HomeFragment.java (This is the class that receives the changed settings)

final SharedPreferences sharedPreferences = getActivity().getSharedPreferences("CURRENCY_SHARED", 0);;

        //if-else branch to select currency
        String currentCurrency = sharedPreferences.getString("CURRENCY", "");

        currentCurrencyTv.setText(currentCurrency);
        if (currentCurrency == "US Dollars") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_money, 0, 0, 0);
        } else if (currentCurrency == "Japanese Yen") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_yen, 0, 0, 0);
        } else if (currentCurrency == "Euro") {
            currentCurrencyTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_euro, 0, 0, 0);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文