I want to create a unique Id with reset button. I have a class ViewId
.ViewId
class contain AtomicInteger
. AtomicInteger
create unique Id successfully.But I want to reset unique Id using a button.So please help me to reset and create unique id again.
ViewId
Class :
import java.util.concurrent.atomic.AtomicInteger;
public class ViewId {
private static ViewId INSTANCE = new ViewId();
private AtomicInteger seq;
private ViewId() {
seq = new AtomicInteger(0);
}
public int getUniqueId() {
return seq.incrementAndGet();
}
public static ViewId getInstance() {
return INSTANCE;
}
}
MainActivity
Class :
public class MainActivity extends AppCompatActivity {
EditText editValue;
ViewId viewId = ViewId.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editValue = (EditText)findViewById(R.id.editText);
}
// Increment Button
public void floatIncrement(View view) {
Log.e("Incremented value is : ", String.valueOf(viewId.getUniqueId()));
}
// Reset Button
public void resetBtn(View view) {
// reset here
}
}
Just call seq.set(0)
, which should just bring your AtomicInteger
back to 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With