i'm new to kotlin android studio,trying to update the textView with editText from data class and i used in editText "afterTextChanged" , created a setter function in data class with editable paramete, i can't convert double to string with editable, anyone help me out ?
here is function in data class
fun setSize(editable: Editable) {size = editable.toString()}
Your size variable is (apparently) a Double, so you have to assign a Double value to it. You're doing editable.toString() which just gives you a String representation of your Editable's contents.
You need to try and parse that as a Double with toDouble() (which throws an exception if it's not a valid number) or toDoubleOrNull (which returns null if it's invalid).
Since it's (I assume) user-edited content you're parsing, and there's a good possibility they'll enter something that's not a valid number representation, you need to handle that possibility. I'd go with toDoubleOrNull since it's easier in Kotlin (there are lots of these *OrNull variants on functions for that reason):
fun setSize(editable: Editable) {
// only runs the let block if the value is parsed
editable.toString().toDoubleOrNull()?.let { size = it }
}
or if you're not familiar with let:
fun setSize(editable: Editable) {
val double = editable.toString().toDoubleOrNull()
if (double != null) size = double
}
I am comparing a string value from selected string value from spinner. However, even if the string that I am testing is the same string value from spinner it always return false. I have tested different ways, simplifying the conditions and it always ends the same. The printed value in the log is the same with the string, so why does it always return false?
final Spinner spinner_familyTest = (Spinner) findViewById(R.id.spinner_family);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.family_array, R.layout.spinner_layout);
adapter.setDropDownViewResource(R.layout.spinner_layout);
spinner_familyTest.setAdapter(adapter);
// Value of familyTest from spinner as printed in the log is "Apiaceae"
familyTest = spinner_familyTest.getSelectedItem().toString();
if (familyTest == "Apiaceae") {
Log.i(TAG, "This is True!");
}
Log.i(TAG, "This is False");
I had faced the same issue some time back. The trick was to use equals() instead of ==
equals() compares string values whereas == compares string refrences
So what you need to do is:
if (familyTest.equals("Apiaceae"))
{
Log.i(TAG, "This is True!");
}
The MFC CComboBox allows a person to do an AddString of an empty string. I just proved that by doing a GetCount() before and another after the AddString; Count was 0, then it became 1; and the GUI also seems to reflect it, as its list was an huge empty box, and when adding it became a one-liner.
I also proved it further by doing
int a = m_combo.GetCount();
CString sx= _T("Not empty string");
if(a == 1)
m_combo.GetLBText(0, sx);
TRACE(_T("Start<%s>End"), sx);
and the Output window displays
File.cpp(9) : atlTraceGeneral - Start<>End
so we conclude the sx variable is empty.
Then I do a FindString having a CString m_name variable which is empty:
int idx= m_combo.FindString(-1, m_name);
And it returns CB_ERR!
Is it standard behavior for empty string entries? Official documentation doesn't say anything about it!
If it is, what is the simplest way to override it? Is there some parameter or change in the resources to change the behaviour? If there is not, I am thinking about deriving or composing a class just for the case where the string is Empty!
I did it manually for the empty string and it works!
CString sItem;
int idx_aux= CB_ERR;
// need it because FindString, FindStringExact and SelectSring return CB_ERR when we provide an empty string to them!
if(m_name.IsEmpty())
{
for (int i=0; i<m_combo.GetCount(); i++)
{
m_combo.GetLBText(i, sItem);
if(sItem.IsEmpty())
{
idx_aux= i;
break;
}
}
}
else
idx_aux= m_combo.FindString(-1, m_name);
Why is the output of the given 200 and not 20000 as I had expected ????
Please help me on this !!!
class Program
{
static void Main(string[] args)
{
mukul x = new mukul();
x.b= 200;
Console.WriteLine(Convert.ToString(x.calculate));
Console.ReadLine()
}
}
class mukul
{
public int b;
public int calculate
{
get { return b; }
set { b = value * 100; }
}
}
You're setting x.b directly - you're not using the calculate setter, so it's not multiplying by 100.
If you changed this line:
x.b = 200;
to this:
x.calculate = 200;
then it would act as you expect.
There are several points to make though:
If you indent your code properly, it will make it easier to read
If your b field were private, you couldn't have set it directly. Fields should almost always be private.
You should follow normal .NET naming conventions (PascalCase for properties and types, for example)
Your property is very odd. It's very unusual for code like this:
x.calculate = x.calculate;
to actually make a difference. I would rethink your design if I were you.
If you're calling Console.WriteLine, there's already an overload to handle an int value, and even if there weren't the value could be boxed to object instead. So your code would be written more simply as:
Console.WriteLine(x.calculate);
In this " x.b= 200;" you calling only "b" variable.not "calculate" method.So every time you get the 200 as output with out performing calculate method.
For better understanding
Take two breakpoints at the below two instructions
get { return b; }
set { b = value * 100; }
Then perform stepinto debug(press F11) for the two instructons like "x.b=200" and "x.claculate=200".Then observe the difference between "x.b=200" and "x.claculate=200"
In x.b=200
"set{b=value*100;}"method cant execute.That means b=value*100 not executed.So every time you get "200" as output.
In x.calculate=200
"set{b=value*100;}"method is executed.That means b assigned with value*100.So you get "20000" as output.
Finally you have to call method("calculate") not variable("b").
Hope you got the answer.Have a happy Programming........
I am supposed to implement a gesture-based menu in which you scroll through a horizontal list of items by panning or flinging through them. This kind of menus is very common in smart phone games. Example case would be Cut the Rope where you select box (Cardboard box, Fabric box) or Angry Birds where you select the set of levels (Poached Eggs, Mighty Hoax).
What I am thinking is that I'll have to do some complex physics calculations and give velocities and accelerations to menu items based on the gestures. Any better solutions? I am using libgdx btw.
I don't think you'd need to go through all that to implement a simple menu! It's all about defining offsets for various items (I'll just assume you want Cut the Rope-style menus, with only one entry in sight at a given moment (excluding transitions)) and then tweening between those offsets whenever a flick is detected!
You seem to have the gesture system all wired up, so right now, we just need to figure out how to display the menu. For simplicity's sake, we'll just assume that we don't want the menu to wrap around.
We'll start by envisioning what this menu will look like, in our heads. It would be just like a filmstrip which passes through the phone and can be seen through the screen.
phone ("stuff" is currently selected)
========
|---------------| |-------|
| Start | About | Stuff | Quit |
|---------------| |-------|
| |
| |
| |
========
We'll just assume that the screen width is w and, consequently, all menu entries are exactly that width (think Cut the Rope again!).
Now, when "Start", is to be displayed, we should just render the flimstrip on the screen starting with the first element, "Start", while the rest would, theoretically, lie to the right of the screen. This will be considered the basic case, rendering the menu with the offset = 0.
Yes, yes, this offset will be the key to our little slidey-slidey menu! Now, it's pretty obvious that when about is selected, we'll just have to offset the "filmstrip" to the left by one "frame", and here offset = - 1 * frameWidth. Our example case illustrated by my brilliant ASCII art has the third menu item selected, and since the frames are indexed starting from 0, we'll just subtract two times the frameWidth and get the desired offset. We'll just render the menu starting at offset = -2 * frameWidth.
(Obviously you can just compute frameWidth in advance, by using the API to fetch the screen width, and then just drawing the menu element text/ graphic centered).
So this is pretty simple:
the user sweeps to the left, we need to get to the menu closer to offset 0, we reduce the index of the selected entity by one and the menu then jumps to the right position
the user sweeps to the right, we increase the index (obviously as long as it doesn't go over the number of menu elements - 1)
But what about smooth tweens?
Libgdx thankfully has interpolations all set for nice little tweens. We just need to take care of a few things so we don't shoot ourselves in the leg. I'll list them here.
One quick note:
The Cut the Rope level selector works a tad differently than what I'm saying here. It doesn't just react to flicks (pre-defined gestures), rather it's more sensitive. You can probably achieve a similar effect by playing with offsets and tracking the position of the finger on the screen. (If the user dragged a menu entry too much to the left/right, transition to the previous/next automatically) Friendly advice: just set up a simple, working menu, and leave details like this towards the end, since they can end up taking a lot of time! :P
Alright, back on track!
What we have now is a way to quickly switch between offsets. We just need to tween. There are some additional members that come into play, but I think they're pretty self-explanatory. While we're transitioning between two elements, we remember the "old" offset, and the one we're heading towards, as well as remembering the time we have left from the transition, and we use these four variables to compute the offset (using a libgdx interpolation, exp10 in this case) at the current moment, resulting in a smooth animation.
Let's see, I've created a quick'n'dirty mock-up. I've commented the code as best as I could, so I hope the following snippet speaks for itself! :D
import java.util.ArrayList;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
public class MenuManager {
// The list of the entries being iterated over
private ArrayList<MenuEntry> entries = new ArrayList<>();
// The current selected thingy
private int index;
// The menu offset
private float offset = 0.0f;
// Offset of the old menu position, before it started tweening to the new one
private float oldOffset = 0.0f;
// What we're tweening towards
private float targetOffset = 0.0f;
// Hardcoded, I know, you can set this in a smarter fashion to suit your
// needs - it's basically as wide as the screen in my case
private float entryWidth = 400.0f;
// Whether we've finished tweening
private boolean finished = true;
// How much time a single transition should take
private float transitionTimeTotal = 0.33f;
// How much time we have left from the current transition
private float transitionTimeLeft = 0.0f;
// libgdx helper to nicely interpolate between the current and the next
// positions
private Interpolation interpolation = Interpolation.exp10;
public void addEntry(MenuEntry entry) {
entries.add(entry);
}
// Called to initiate transition to the next element in the menu
public void selectNext() {
// Don't do anything if we're still animationg
if(!finished) return;
if(index < entries.size() - 1) {
index++;
// We need to head towards the next "frame" of the "filmstrip"
targetOffset = oldOffset + entryWidth;
finished = false;
transitionTimeLeft = transitionTimeTotal;
} else {
// (does nothing now, menu doesn't wrap around)
System.out.println("Cannot go to menu entry > entries.size()!");
}
}
// see selectNext()
public void selectPrevious() {
if(!finished) return;
if(index > 0) {
index --;
targetOffset = oldOffset - entryWidth;
finished = false;
transitionTimeLeft = transitionTimeTotal;
} else {
System.out.println("Cannot go to menu entry <0!");
}
}
// Called when the user selects someting (taps the menu, presses a button, whatever)
public void selectCurrent() {
if(!finished) {
System.out.println("Still moving, hold yer pants!");
} else {
entries.get(index).select();
}
}
public void update(float delta) {
if(transitionTimeLeft > 0.0f) {
// if we're still transitioning
transitionTimeLeft -= delta;
offset = interpolation.apply(oldOffset, targetOffset, 1 - transitionTimeLeft / transitionTimeTotal);
} else {
// Transition is over but we haven't handled it yet
if(!finished) {
transitionTimeLeft = 0.0f;
finished = true;
oldOffset = targetOffset;
}
}
}
// Todo make font belong to menu
public void draw(SpriteBatch spriteBatch, BitmapFont font) {
if(!finished) {
// We're animating, just iterate through everything and draw it,
// it's not like we're wasting *too* much CPU power
for(int i = 0; i < entries.size(); i++) {
entries.get(i).draw((int)(i * entryWidth - offset), 100, spriteBatch, font);
}
} else {
// We're not animating, just draw the active thingy
entries.get(index).draw(0, 100, spriteBatch, font);
}
}
}
And I believe a simple text-based menu entry that can draw itself would suffice! (do mind the dirty hard-coded text-wrap width!)
public class MenuEntry {
private String label;
// private RenderNode2D graphic;
private Action action;
public MenuEntry(String label, Action action) {
this.label = label;
this.action = action;
}
public void select() {
this.action.execute();
}
public void draw(int x, int y, SpriteBatch spriteBatch, BitmapFont font) {
font.drawMultiLine(spriteBatch, label, x, y, 400, HAlignment.CENTER);
}
}
Oh, and Action is just a thingy that has an execute method and, well, represents an action.
public interface Action {
abstract void execute();
}
Feel free to ask any related question in the comments, and I'll try to clarify what's needed.
Hope this helps!