开发者

Android Spinner

I am working in Spinner.. I am having three spinners and i am going to display days (i,e) 1,2,3,4,5...30,31 in 1st spinner and months in 2nd spinner (i,e)1,2,3,...11,12 and Year in 3rd spinner 2010,.... What i want is that that i wanna to display current date,month,year in 1st,2nd,3rd spinner resp when the page loads..from that it is possible to select any day,month,Year Here is my code::

public class NoteEdit extends Activity implements OnItemSelectedListener {

private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private NotesDbAdapter mDbHelper;
private String array_spinner[];
private int currentDayOfMonth=0;
private int currentDayOfDays=0;
private final String[] months = {"1","2","3","4","5","6","7","8","9","10","11","12"};
Spinner s1,s,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    setContentView(R.layout.note_edit);

     Calendar cal = Calendar.getInstance();
     cal.set(Calendar.MONTH,month);//month starts from 0
     cal.set(Calendar.YEAR,year);
     int noOfDayInMonthyear = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
     Log.v("noOfDayInMonthyear",""+noOfDayInMonthyear);

    ArrayList<Integer> obj=new ArrayList();
    ArrayList<Integer> obj1=new ArrayList();
      for(int i=1;i<=noOfDayInMonthyear;i++){
          obj.add(i); 
      }
      Log.v("Size",""+obj.size());
      for(int i=2010;i<=2020;i++){
          obj1.add(i); 
      }
      Log.v("Size1",""+obj1.size());
         s = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,obj);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(NoteEdit.this);

         s1 = (Spinner) findViewById(R.id.Spinner02);
        ArrayAdapter adapter1 = new ArrayAdapter(this,android.R.layout.simple_spinner_item,months);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter1);
        s1.setOnItemSelectedListener(NoteEdit.this);

     s2 = (Spinner) findViewById(R.id.Spinner03);
    ArrayAdapter adapter2 = new ArrayAdapter(this,android.R.layout.simple_spinner_item,obj1);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s2.setAdapter(adapter2);
    s2.setOnItemSelectedListener(NoteEdit.this);
    s2.setAdapter(adapter2);



    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);

    Button confirmButton = (Button) findViewById(R.id.confirm);

    mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID) 
                                        : null;
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();            
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) 
                                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });
}


public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

}


public void onNothingSelecte开发者_如何学运维d(AdapterView<?> arg0) {


}


if i understand your question right, i think you're asking how to set the value for each spinner?

figure out what value you want to select for each spinner and do something like this for each http://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int, boolean)

 int dateId = <date id in the spinner>;
 s2.setSelection(dateId);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜