我创建了一个购物车应用程序,它有一个菜单项。然后,它还有另一个名为“ SettingActivity”的活动,为此settingActivity将具有一个TextView,以显示“主要语言,次要语言和这两种语言”。

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

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

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

设置活动:

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/

10-10 12:39