Reputation: 4234
I want to update a TextView in main activity from inside a custom object.
The custom object is a number picker written from scratch. It has an EditText and 2 button (+/-). My purpose is that when a user press the + or - button the TextView will be updated with that value.
Now i can think 2 possible solutions:
But my problem is that i want to keep the textView logic outside the custom picker (so if is possible to have the update function inside the activity). There is a solution to that problem?
The custompicker xml code is that:
<org.croncalc.MyPicker android:id="@+id/hourpicker"
android:orientation="vertical"
android:layout_width="65dp"
croncalc:maxnumber="24"
croncalc:minnumber="1"
croncalc:step="5"
croncalc:cycle="true"
android:layout_height="wrap_content">
I want that everytime one of the picker change its value the textview on the right corner will be updated.
If useful here the onClick() code for the +/- buttons:
OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.decbtn:
step(-1);
break;
case R.id.incbtn:
default:
step(1);
break;
}
}
};
private void step(int step) {
current += step;
if (step > 0) {
decButton.setEnabled(true);
if ( current > maxNumber) {
if(cycle){
current-=maxNumber;
} else {
current=maxNumber;
incButton.setEnabled(false);
incButton.setPressed(false);
}
}
} else {
incButton.setEnabled(true);
if ( current < minNumber) {
if(cycle){
current+= maxNumber;
} else {
current=minNumber;
decButton.setEnabled(false);
decButton.setPressed(false);
}
}
}
pickerText.setText(Integer.toString(current));
}
Thanks in advance. Ivan
Upvotes: 0
Views: 519
Reputation: 4234
Ok i answer myself (i found a solution).
I define a CustomListener for my objcet.
I declared an interface called:
package org.croncalc;
public interface OnMyPickerChangeListener {
public abstract void onMyPickerChanged(String srcTxt);
}
and then i added in MyPickerClass (this is the name of the object) a new class Variable:
OnMyPickerChangeListener onMyPickerChangeListener;
That variable will be initialized at null. After i defined in MyPicker two new methods:
The first is the setOnMyPickerChangeListener with that method i can add a OnMyPickerChangeListener:
public void setOnMyPickerChangeListener(OnMyPickerChangeListener listener) { onMyPickerChangeListener = listener; }
The second method is onMyPickerChanged, that method will be called everytime we want to trigger the Change Event:
private void onMyPickerChanged(){ if(onMyPickerChangeListener!=null){ onMyPickerChangeListener.onMyPickerChanged(pickerText.getText().toString()); } }
Now in the main activity i declared a new OnMyPickerChangeListener and implemented the interface method onMyPickerchanged:
OnMyPickerChangeListener pickerListener = new OnMyPickerChangeListener() {
@Override
public void onMyPickerChanged(String srcTxt) {
localTv.setText("Value: " + srcTxt);
}
};
And i added that Listener to the Picker:
minutesPicker.setOnMyPickerChangeListener(pickerListener);
And now it works :)
Upvotes: 0
Reputation: 11975
TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,int hourOfDay, int minute) {
//Update Here textView when you click on dialog
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
Hope it Help you
Upvotes: 1