gpt4 book ai didi

java - 如何将SharedPreferences数据从settingActivity传递到MainAdapter?

转载 作者:行者123 更新时间:2023-12-01 16:22:13 27 4
gpt4 key购买 nike

我创建了一个购物车应用程序,它有一个菜单项。然后它有另一个名为“SettingActivity”的 Activity ,为此设置 Activity 将有一个 TextView 来显示“主要语言、次要语言和两种语言”。

我在SettingActivity中获取了SharedPreferences数据。如何将这些数据传递给 MainAdapter?

因为当我在SettingActivity中单击第二语言时,菜单将更改为第二语言或显示两种语言。

那么,如何在MainAdapter中获取SettingActivity的SharedPreference数据呢?

设置 Activity :

public class SettingActivity extends AppCompatActivity {
LinearLayout languageModeLinearLayout;
TextView languageModeTextView;
SharedPreferences settingPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
languageModeLinearLayout = findViewById(R.id.setting_language_mode_linearLayout);
languageModeTextView = findViewById(R.id.setting_language_mode_textView);
settingPreferences = getSharedPreferences("setting", MODE_PRIVATE);

updateUI();

languageModeLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] language = getResources().getStringArray(R.array.setting_language_mode);
AlertDialog.Builder languageMode = new AlertDialog.Builder(v.getContext());
languageMode.setTitle("Select Language Mode");
languageMode.setItems(language, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
SharedPreferences.Editor editor = settingPreferences.edit();
editor.putInt("language", position);
editor.apply();
updateUI();
}
});

languageMode.create().show();
}
});


}

private String getLanguage(){
String[] language = getResources().getStringArray(R.array.setting_language_mode);
int position;
position = settingPreferences.getInt("language", 0);

return language[position];
}

private void updateUI(){
languageModeTextView.setText(getLanguage());
}

主适配器:

public class MainAdapter extends ArrayAdapter<MenuItem> implements Serializable {
private TextView mCartCounterTextView;

public MainAdapter(Context context, List<MenuItem> menuItems) {
super(context, 0, menuItems);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View itemView = convertView;
final ViewHolder viewHolder;

if (itemView == null) {
itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_main, parent, false);
viewHolder = new ViewHolder(itemView);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}

final MenuItem currentMenuItem = getItem(position);
viewHolder.nameTextView.setText(currentMenuItem.getPrimaryName());
viewHolder.nameTextView.setText(currentMenuItem.getSecondaryName());

viewHolder.addToCartButton.setText(getFormattedPriceTextToButton(currentMenuItem));
viewHolder.addToCartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Main.getAddedItems().add(currentMenuItem);
Toast.makeText(v.getContext(), "Item had added to cart", Toast.LENGTH_SHORT).show();

mCartCounterTextView.setText(String.valueOf(Main.getAddedItems().size()));
mCartCounterTextView.setVisibility(View.VISIBLE);
}
});
return itemView;
}

private String getFormattedPriceTextToButton(MenuItem currentMenuItem) {
return "+ $" + currentMenuItem.getPrice();
}

private class ViewHolder {
TextView nameTextView;
TextView priceTextView;
Button addToCartButton;

public ViewHolder(View itemView) {
nameTextView = itemView.findViewById(R.id.main_name_textView);
priceTextView = itemView.findViewById(R.id.main_price_textView);
addToCartButton = itemView.findViewById(R.id.main_add_to_cart_button);
}
}

public void setData(TextView cartCounter){
mCartCounterTextView = cartCounter;
}

private void getSharedPreferencesLanguage(){
// SharedPreferences.Editor editor = getContext().getSharedPreferences("setting", MODE_PRIVATE).edit();
SharedPreferences language = getContext().getSharedPreferences("setting", MODE_PRIVATE);
}

菜单项:

public class MenuItem {
private String mPrimaryName;
private String mSecondaryName;
private double mPrice;

public MenuItem(String primaryName, String secondaryName, double price){
mPrimaryName = primaryName;
mSecondaryName = secondaryName;
mPrice = price;
}

public String getPrimaryName(){
return mPrimaryName;
}
public String getSecondaryName(){
return mSecondaryName;
}
public double getPrice(){
return mPrice;
}

最佳答案

只需将您的上下文传递给函数并从sharedpref获取值

    public static String getLan(Context cont) {
SharedPreferences settingPreferences=settingPreferences =cont.getSharedPreferences("setting", MODE_PRIVATE);
String[] language = getResources().getStringArray(R.array.setting_language_mode);
int posi =settingPreferences.getInt("language",0);
return language[pos];
}

关于java - 如何将SharedPreferences数据从settingActivity传递到MainAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62239944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com