嗨..这是我的代码我想在我的微调器中添加值..但我不知道我是怎么做到的.. ex A + = 5 等待你的帮助
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private Spinner spinner1, spinner2;
private Button f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
EditText e1 = (EditText) findViewById(R.id.o1);
Button m = (Button) findViewById(R.id.b22);
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("A+");
list.add("A");
list.add("B");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list1 = new ArrayList<String>();
list1.add("A+");
list1.add("A");
list1.add("B");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button f=(Button)findViewById(R.id.button);
f.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int gr1=Integer.parseInt(e1.getText().toString());
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
printf(“%d \ n”,42); /* 什么是 再问一次? * / 我不知道我应该在哪里添加在spinner中添加值的代码
答案 0 :(得分:3)
public class MainActivity extends Activity {
private SharedPreferences prefs;
private String prefName = "spinner_value";
int id=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final Spinner sp=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adp);
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
id=prefs.getInt("last_val",0);
sp.setSelection(id);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("last_val", pos);
//---saves the values---
editor.commit();
Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
答案 1 :(得分:1)
尝试类似的东西:
testSpinner = (Spinner) findViewById(R.id.testSpinner);
ArrayList<String> list = <value come from somewhere>
testSpinner = (Spinner) findViewById(R.id.testSpinner);
MyAdapter testAdapter = new MyAdapter(getApplicationContext(), R.layout.spinner_style, list);
testSpinner.setAdapter(testAdapter);
testSpinner.setSelection(testAdapter.getCount());