Reputation: 451
I'm trying to populate a NumberPicker on a custom made dialog. At the moment I'm using a method which uses a View and inflates another layout (the dialog), the method should then populate the NumberPicker but when I call it in onCreate it doesn't.
The code for the method:
public void fillArray() {
View inflatedView = getLayoutInflater().inflate(R.layout.add_activity, null);
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
String[] hoursArray = new String[25];
String[] minutesArray = new String[61];
for( i = 0; i < hoursArray.length; i++ ) {
hoursArray[i] = Integer.toString( i );
}
for( x = 0 ; x < minutesArray.length; x++ ){
minutesArray[x] = Integer.toString( x );
}
np_hours.setMinValue( 0 );
np_hours.setMaxValue( 24 );
np_hours.setWrapSelectorWheel( false );
np_hours.setDisplayedValues( hoursArray );
np_minutes.setMinValue( 0 );
np_minutes.setMaxValue( 60 );
np_minutes.setWrapSelectorWheel( false );
np_minutes.setDisplayedValues( minutesArray );
}
EDIT:
The idea of this is to pick the duration of something (not the time) - sorry if this caused confusion!
Upvotes: 1
Views: 590
Reputation: 4899
Since you just want consecutive numbers there's no need to create the side arrays.
You can do it as follows:
public void fillArray() {
View inflatedView = getLayoutInflater().inflate(R.layout.add_activity, null);
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_hours.setMaxValue(24);
np_hours.setMinValue(0);
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
np_minutes.setMaxValue(60);
np_minutes.setMinValue(0);
}
If this is not working please post your XML layout and the full activity code (at least the onCreate() method).
Reference:
Update
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.plan_activity );
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate( R.menu.main, menu );
return true;
}
@SuppressLint("NewApi")
public void adActivity( View view ) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View parentView = inflater.inflate(R.layout.add_activity, null);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(parentView);
builder.setPositiveButton(R.string.add_activity, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Stert
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
fillArray(parentView);
// Create the AlertDialog object and return it
AlertDialog mainAlert = builder.create();
mainAlert.show();
}
// Method to fill the NumberPicker's
public void fillArray(View view) {
View inflatedView = view;
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_hours.setMaxValue(24);
np_hours.setMinValue(0);
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
np_minutes.setMaxValue(60);
np_minutes.setMinValue(0);
}
Upvotes: 2