Spinner Android (SetPrompt) - android

I am running into an issue with a spinner that I defined and bound to an array resource. The problem is that it "ONLY" defaults to the first item of the array when it is first constructed. I am using the setPrompt and it looks like it is being ignore totally. I wrote to the log and I can see in the log that I am setting it to the right value but it instead keeps defaulting to the first element in the array.
_spnCountDown.setPrompt(setting);
Log.d("SETTING_SPINNER", setting);
_spnCountDown.setOnItemSelectedListener(new OnItemSelectedListener()
{
boolean _firstTime = true;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
if (_firstTime == false)
{
String value = _spnCountDown.getSelectedItem().toString();
MobileAppManager.getInstance().storeSetting("CountDown",
value);
Log.d("SETTING_SPINNER onItemSelected", value);
}
else
{
Log.d("SETTING_SPINNER onItemSelected", "Ignore");
_spnCountDown.setPrompt(Settings.this.getInitialCountDown());
_firstTime = false;
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
I have followed few answers that recommend using a flag to overcome the fact that onSetItemSelected will first the first time the spinner is constructed. So, rightfully so, I am ignoring the first call. However as I mentioned, It is defaulting to first entry.
So, If this line will not do anything _spnCountDown.setPrompt("5 seconds")

I'm not sure but if I understand your question correctly this suggests perhaps you should use setSelection?
Setting default values in spinner in android

Related

Applying auto-calculation to edit text

If the user enters any value like 1234 in the edit text box, then all the values of the edit text box are Addition in each other and the answer is shown in a text box.
For example if user enter values such as 1234 in edit text and after addition (1+2+3+4=10) their answer that is 10 show in text box.
You could probably use data binding, either one or two way depending on how you want to do things.
Make sure to read the documentation around data-binding before starting to successfully implement!
Using one-way data binding, you can set a value on an attribute and set a listener that reacts to a change in that attribute - android docs
First set your binding adapters on your new SumNumberTextView,
Then in your parent view's layout file, if you want to use the more verbose one way data binding, you'd reference something like:
android:numbers="#{viewmodel.numbers}"
and
android:onNumbersChanged="#{() => viewmodel.onNumbersChanged()}"
Then you can define a function on your view model that does your numeric operations whenever you input numbers.
You can just use textwatcher on edittext to get update when text change and do your operation of addition just like this
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// call function from here if you want to perform operation as user provide input
add(cs.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
}
#Override
public void afterTextChanged(Editable arg0) {
// call function from here if you want to perform operation only user stop input
add(arg0.getText().toString());
}
});
private void add(String input){
if(input != null && input != ""){
String[] numbers = input.split("")
int total = 0;
for(int counter = 0 ; counter < numbers.length ; counter++){
total += Integer.parseInt(numbers[counter]);
}
Log.e("total",total);
}
}

listview - access to non-visible items

I would like to check which of elements on my listview is also a member of another list and check all of them (by changing background). But the only way I can think of is:
for (String str : list1){
if (list2.contains(str)) {
lv.getChildAt(adapter.getPosition(str)).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
But that works only for visible elements of the list and throws null pointer exception when accessing non-visible elements. What can I do to apply changes for all list items? Do I have to write my own adapter or maybe there is any "equivalent" of getChiledAt but working for all elements of the listview not only visible ones?
I didn't try by myself, but a suggestion. Can you please try this way, and check.
Idea is to use setOnScrollListener with onScroll method and to have null check inside for loop.
It's not good solution though, because for loop working on every scroll.
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
for (String str : list1) {
if (list2.contains(str)) {
if (lv.getChildAt(adapter.getPosition(str)) != null) {
lv.getChildAt(adapter.getPosition(str))
.setBackgroundColor(
getResources()
.getColor(
android.R.color.darker_gray));
}
}
}
}
});
The solution was to use onScroll method (as Chitrang suggested) and set everything only for visible item. Android Magic works, everything works fine, also for non-visible items :)
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
for (int i = lv.getFirstVisiblePosition(); i<=lv.getLastVisiblePosition(); i++){
if (list2.contains(lv.getItemAtPosition(i).toString()))
lv.getChildAt(i - lv.getFirstVisiblePosition()).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
}
You can create a new List, with the (i assume you have) id like the listview's data.
Example :
You have class Person with int id, String name. Create a new List<Integer> to store all (not just the visible one) of your listview's person id, normally by using the listview's adapter.

Spinner onItemSelected a Action should trigger in for getting data

I am trying this code
findUsStateOrMileSpinnerState.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l)
{
spinnerState = findUsStateOrMileSpinnerState.getSelectedItem().toString();
if(spinnerState.equalsIgnoreCase("State")){
getDetailsState();
}
if(spinnerState == "Miles"){
getDetailsMiles();
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
}
);
On some selected item it should call another listener.
Spinner is having Miles and State.
But it is not going through the if statement, am I doing something wrong.
Looking forward to your reply.
thanks.
Did you try using the eqauls() method for comparing two strings in your code instead of == operator ?
if (spinnerState.equals("Miles") {
getDetailsMiles();
...
}
Check this out plz : http://www.java-samples.com/showtutorial.php?tutorialid=221
Try using the getSelectedIndex() instead of getSelectedItem(). You have to be sure you always get "State" and "Miles" at specified index.
You should do string compare via equals and not spinnerState == "Miles"
Try using equalsIgnoreCase or equals instead of using == to compare strings

How to overwrite value in edittext

I have an edittext in my app, which accepts only numbers. It is set to 0 by default. But when i enters some umbers, i want to overwrite the initial 0 in the left. Now i am having some problem with edittext. For example, when i inserts a 1, i want it to how 1, not 01. my code is like,
value = (EditText)findViewById(R.id.value);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(arg0.length()==0)
{
value.setText("0");
int i = value.getText().length();
value.setSelection(i);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
How can i achieve it? or is it possible?
You can set android:hint in your xml file.
For example,
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="0"/>
There are lots of options to accomplish what you're looking for. Besides the ones already mentioned, I'd say the EditText's attribute android:selectAllOnFocus="true" might be worth looking at. It's convenient for a default value, and at the same quite user friendly as changing the value does not require any 'backspacing'.
You can change string to empty in your beforeTextChanged Function Call and let the onTextChanged as it is.
Instead of using value.setText("0");
use value.setHint("0");
Use setHint(int) method to display 0 initially. So that when the user types something it is automatically cleared and you can get the text as desired.

how to set a default value to EditField when it is cleared?

I am using an EditText field in my application. also i had added addTextChangedListener for that. On the onTextChanged method, i had called my squareFunction. The code is like..
value = (EditText)findViewById(R.id.amount);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
squareFunction();
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
Now my problem:= i am getting the value of input numbers correctly. But when i clear the EditField, an error comes and ask me to "Force Close". I want to make my editField value to 0 if all the characters in it are cleared. Is it possible? If yes, how it can be done. Plz explain with code if possible...
inside onTextChanged()
if( arg0.length()==0)
{
value.setText("");
}
i hope you tried this basic thing , so what errorlog says at the time of force close ??
You should try adding condition like
if(value.getText().toString().equalsIgnoreCase(""))
{
Log.i(TAG,"EditText Clear");
//set value to 0
}
else
{
squareFunction();
}
also use this in afterTextChanged
The error is due to the function trying to perform square when you r clearing the values. Move the square function to afterTextChanged, and in onTextChange, check the length of the edittext, if it is zero, set text to 0.

Categories

Resources