I am trying to use a NumberPicker to display an array of Strings, but when I try and get the value back of the current String, the value is the integer index value of the String within the array which is the default response.
I have been trying to use NumberPicker.Formatter to get the actual String as a value back as opposed to the integer index value. I have implemented this, but it doesn't seem to be invoked. Could someone please tell me why this is and what I can do to fix it. Thanks in advance.
Here is the code:
final NumberPicker npUnits = (NumberPicker) numberPickerView.findViewById(R.id.numberPicker2);
npUnits.setMinValue(1);
npUnits.setDisplayedValues(tableUnitsArray);
npUnits.setMaxValue(tableUnitsArray.length);
npUnits.setFormatter(new NumberPicker.Formatter()
{
#Override
public String format(int value)
{
ArrayList<String> stringArrayList = (ArrayList<String>) Arrays.asList(tableUnitsArray);
String defaultUnits = stringArrayList.get(value);
System.out.println("Value formatted result: " + defaultUnits);
return defaultUnits;
}
});
npUnits.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
NumberPicker allows you to pick your values based on the min and max values you specify. It does not need you to call setDisplayedValues(). NumberPicker uses the String array you specify as a set of alternate values which causes your formatter to be ignored. Try removing the call to setDisplayedValues().
If your values for your NumberPicker are not so straight forward (i.e not from min to max incrementing by 1), then you can transform your values in your formatter to get the desired number.
I have found out the answer to my question. NumberPicker.Formatter was the wrong thing that I was doing as I didn't actually need to format anything. I just needed to get the current value from the String[] displayed values.
This is the code that worked for me:
List<String> stringArrayList = (List<String>) Arrays.asList(npUnits.getDisplayedValues());
String defaultUnits = stringArrayList.get(npUnits.getValue() - 1);
I found a solution for a few bugs in NumberPicker that works in APIs 18-26 without using reflection and without using setDisplayedValues() here.
Related
I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);
I have an arraylist of type string which is assigned to spinner. When I use following code to set spinner's selection, it returns -1 as position although string is present in the list.
int spinnerPosition = myAdap.getPosition(myString);
////here it returns -1
//set the default according to value
spinner.setSelection(spinnerPosition);
It returns correct position for some adapters but not for others. What can be possibly wrong?? String I passed in "myString" variable is 100% correct and exists in the adapter list.
I'm not positive, so I apologize if this is wrong. Don't have source code to look at right now.
but I think the getPosition() method does not perform a .equals() comparison, so you can't just pass in an equivalent string. So don't do
myString = "asdf"; //myString is getting assigned a reference to a brand new string
spinnerPosition = adapter.getPosition(myString);
because mySpring is not pointing at the same object as the string in your array, even if myString.equals(originalString) may return true;
This should work:
//assign the reference to point at the exact same object that is in the ArrayList
myString = originalString;
spinnerPosition = adapter.getPosition(myString);
I've a string value String str="Success"
How to check the "str" value is not equal to "Success". If I try if(str!="Success"). It is not working properly.
I've a integer value int a=250 If I use If(a!=250) this is also not working properly.
How to code for these conditional statements ? I'm using Android and Eclipse version 2.1.
Any help would be appreciable.
a!=250 should work.
For string try str.equals("Success")
As every one said, String values should compare with .equals method.
like
if(str.equals("Success"))
{
System.out.println("My String is Success");
}
Next coming to integer comparison.
As you have said, you have the integer value
int a=250
if(a!=250)
{
// Some code
}
Here already your a value is 250. then you are executing some lines of code if a value is not equal to 250. Then how the condition will execute. if you want to test then
change the a value and then check again as
int a=50
if(a!=250)
{
// Some code===============> Now this code will execute
}
For string comparison, use the standard String.equals() method.
str.equals("Success"); - For Equals
if(!str.equals("Success")) - For Not Equals(your case)
And for int, what is being used, is proper. a!=250 will return false in your case. Hence, it'll not enter the if block.
You should try to study Java first ;)
This is not the way to do a String comparison. For String comparison, you must use .equals(String) method.
String str1 = "str1";
String str2 = "str2";
if(str1.equals(str2)) {
//do something
}
You should not compare strings with != or =. That won't work in most cases.
new String("test").equals("test")
Should be used for comparison.
!= compares the value and is true if they are NOT equal.
you could also use something like if(a == 250){...}
In Java you cannot use == to compare Strings, you must use:
if(string.equals("example"))
So let's use equals() in your conditional and optimize it:
if(!str.equals("Success"))
this will work
I would like to ask how can I get the value of predefined value to its name
The following is my code
public class Calculation_Activity extends Activity{
int a=1;
int b=2;
int c=50;
int result;
String array1[]=new String[]{"a","b","c"};
}
I would like to ask how can I get the value of the string by using array1[i]?
for instance, I would like to use array1[3]to call the value of c[ie.50]
May you give me some advice on this matter?
You might solve your issue by using a Map and its standard implementation HashMap:
Map<String, Integer> values = new HashMap<String, Integer>();
values.put("a",1);
values.put("b",2);
values.put("c",50);
String array1[] = new String[] {"a","b","c"};
int result = values.get(array1[2]); //result = 50
// or
int result = values.get("c"); //result = 50
You can use a HashMap (http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html), is a dictionary like data structure where you can store key-pair values
What you're trying to achieve would be better suited to a dynamic/scripting language. Have you considered using a Map instead of multiple varaibles?
In Java, this is not a common approach, such as it would be in scripting languages. You could try to use a Map (ie HashMap), which would enable you to achieve what you want, sort of.
In fact I think it is possible to do exactly what you want using reflection in Java, but I would not go there!
I have written some code for autocompletetextview in custom dialog box.When typing some text that text dynamically search into the hashmap.This hashmap is with large lines of text.It works.But slowly giving me result.
AutoCompleteTextView searchText = (AutoCompleteTextView)searchDialog.findViewById(R.id.searchText);
if(searchText.getText()!=null){
// searchString = searchText.getText().toString().trim();
String[] autoList = getAutoCompletWords(searchText.getText().toString().trim());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx,android.R.layout.simple_dropdown_item_1line,autoList);
searchText.setAdapter(adapter);
}
private String[] getAutoCompletWords(String text){
Set<String> wordsSet = new TreeSet<String>();
Pattern wordPattern = Pattern.compile("\\b"+text+"\\w+",Pattern.CASE_INSENSITIVE);
Matcher matcher = wordPattern.matcher(bookContentMap.values().toString());
while(matcher.find()){
wordsSet.add(matcher.group());
}
String[] wordsArray = wordsSet.toArray(new String[0]);
return wordsArray;
}
If I take thread for above code it is giving me thread handler exception.Please give me an idea for quick response of list on autocomplettext.
Rajendar Are
To know for sure which bits are fast and which are slow, you need to use Android's profiler. Here are two things worth investigating, they're probably the largest resource drain:
You're compiling a regular expression each time a key is pressed, this is very slow. A better option would be to populate a database and query it instead.
You're creating both a TreeSet and an Array each time a key is pressed, which probably hurts.
Converting bookContentMap's values to a String is probably quite processor intensive. Consider caching this value.