我想保存微调器值并检索所选项的值 问题是我有一个整数旋转器和两个旋转器长 而且我不知道如何使用这种方法获得长期值 我想让这个方法能够以相同的方法设置整数和长期,并且我将这些val保存到sp,并获得它
private void spinnerVal(int spinnerID, int move, int selection){
Spinner sp = (Spinner) findViewById(spinnerID);
ArrayList<Integer> spinnerOption = new ArrayList<>();
for (int i = 0; i < 50; i+=move) {
spinnerOption.add(i);
}
ArrayAdapter<Integer> integerArrayAdapter = new ArrayAdapter<>(this, R.layout.spinner_item, spinnerOption);
integerArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
sp.setAdapter(integerArrayAdapter);
sp.setSelection(selection);
sp.setOnItemSelectedListener(this);
}
这是我的项目选择方法
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Editor editor;
switch (parent.getId()) {
case R.id.spinner1 /*2131492981*/:
myServer.counter1 = Integer.valueOf(parent.getItemAtPosition(position).toString());
editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putInt("counter1", myServer.counter1);
editor.apply();
return;
case R.id.spinner2 /*2131492983*/:
myServer.counter2 = (long) (Integer.valueOf(parent.getItemAtPosition(position).toString()) * 1000);
editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putLong("counter2", myServer.counter2);
editor.apply();
return;
case R.id.spinner3 /*2131492985*/:
myServer.counter3 = (long) (Integer.valueOf(parent.getItemAtPosition(position).toString()) * 1000);
editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putLong("counter3", myServer.counter3);
editor.apply();
return;
default:
return;
}
}
答案 0 :(得分:0)
当Integer.valueOf()
项为Long
时,您正在使用Spinner
来解析Long
值。您必须使用Long.parseLong()
或Long.valueOf()
来解析Long
个项目。