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
Related
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.
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
I want to draw a check mark for the image view I click on and uncheck the imageview I clicked on before using the following code snip. I store last checked position in mDeviceAdapter. When I try to uncheck old position, the image view always gives null even for the partial visible image view. I am really confused because I thought only invisible one is recycled... Newbie in Android and any comment is appreciated.
public void CheckableImageView#setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
invalidate();
}
}
mDeviceGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckableImageView viewToCheck = (CheckableImageView) view;
if (!viewToCheck.isChecked()) {
int oldCheckedPosition = mDeviceAdapter
.getCheckedPosition();
mDeviceAdapter.setCheckedPosition(position);
View checkedView = mDeviceGallery
.getChildAt(oldCheckedPosition);
Log.d(TAG, "old position="+oldCheckedPosition + "old view="+checkedView);
if (checkedView != null) {
((CheckableImageView) checkedView)
.setChecked(false);
Log.d(TAG, "uncheck position="
+ oldCheckedPosition);
}
viewToCheck.setChecked(true);
That's not the right approach.
You need to add to your data type a boolean field (i.e mIsChecked).
On the onItemClick method set the value of that variable to true and keep its INDEX as a member of the adapter. When another item is clicked set the value of that item to true and set the value of the saved one to false (change the value of the datatype in you ArrayList in the INDEX you stored in the previous click).
Now, in the getView() method, you must have if/else statement. Something like:
if (item.isChecked())
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
Example to the onClick method: (just a general direction)
if (item.isChecked())
{
checkedView.setChecked(false);
yourList.get(position).setChecked(true);
yourList.get(mLastCheckedIndex).setChecked(false);
mLastCheckedIndex = position;
notifyDataSetChanged();
}
else
{
//same but opposite.
}
Hope this helps!
I am using the following code to grab the selected value in the spinner:
cbFormato.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int posicao, long id) {
//pega nome pela posição
formatoSelecionado = parent.getItemAtPosition(posicao).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
When I use the code below to show the value that he took the spinner returns the value: Circular
The code to return value of spinner:
Toast.makeText(AppTubulao.this, "Circular: " + formatoSelecionado, Toast.LENGTH_LONG).show();
The problem is when I test the value of the spinner in the same code below it does not recognize the value, ie the value Circular he shows me in Toast is not the same as the "Circular" if that is the test
if (formatoSelecionado == "Circular")
{
Toast.makeText(AppTubulao.this, "Circular: " + formatoSelecionado, Toast.LENGTH_LONG).show();
}
He did not enter the if statement
Assuming they really are set to the same value, the following should evaluate to true.
if (formatoSelecionado.equals("Circular")) {
String equality in Java should use either the equals or equalsIgnoreCase methods. Thus, to test of formatoSelectionado is equal to the string "Circular" you need to use:
if (formatoSelecionado.equals("Circular")) ...
or
if (formatoSelecionado.equalsIgnoreCase("Circular"))
I have an AutoCompleteTextView in my layout. I also have an alternative way to select the same items which are present in the AutoCompleteTextView. When the alternative way is selected, I populate the value in the AutoCompleteTextView via:
autoCompleteTextView.setText(valueFromAlternativeSource);
where valueFromAlternativeSource is one of the valid auto complete options. The trouble with this is that the autocomplete dropdown appears when setText is called. Putting the following line after the above doesn't work:
autoCompleteTextView.dismissDropDown(); //Doesn't work. Why?
Any ideas on why dismiss dropdown isn't working or other ways I could dismiss the dropdown?
This works fine for me and is less complex:
ListAdapter adapter = autoCompleteTextView.getAdapter();
autoCompleteTextView.setAdapter(null);
autoCompleteTextView.setText("whatever");
autoCompleteTextView.setAdapter(adapter);
If you want to support API<17, Subclass AutoCompleteTextview and override setText(text, filter) method
#Override
public void setText(CharSequence text, boolean filter) {
if(Build.VERSION.SDK_INT>=17) {
super.setText(text, filter);
}else{
if(filter){
setText(text);
}else{
ListAdapter adapter = getAdapter();
setAdapter(null);
setText(text);
if(adapter instanceof ArrayAdapter)
setAdapter((ArrayAdapter) adapter);
else
setAdapter((CursorAdapter) adapter);
//if you use more types of Adapter you can list them here
}
}
}
Then whenever you want to set the text manually call setText(text, false)
Looks like its a problem of the order how messages are processed.
My work around looks like this:
//autoCompleteTextView.dismissDropDown();
new Handler().post(new Runnable() {
public void run() {
autoCompleteTextView.dismissDropDown();
}});
autoCompleteTextView.setText(valueFromOtherMeans, filter);
* #param filter If <code>false</code>, no filtering will be performed
* as a result of this call.
My solution (but I don't like it, there must be something better):
autoCompleteTextView.setText(valueFromAlternativeSource);
autoCompleteTextView.setDropDownHeight(0);
autoCompleteTextView.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
autoCompleteTextView.setDropDownHeight(LayoutParams.WRAP_CONTENT);
}
}