How to customize value for numbers in NumberPicker in android? - android

I have a number picker for setting a data limit in MB. right now, I have numberPicker, contains numeric values in sequence like [1,2,3,....., 2000 MB].
But I want a numberPicker that should contain numeric values like [100,200,300,...., 2000MB].
How can I do this?

Displayed array values for your picker
int NUMBER_OF_VALUES = 20; //num of values in the picker
int PICKER_RANGE = 100;
...
String[] displayedValues = new String[NUMBER_OF_VALUES];
//Populate the array
for(int i=0; i<NUMBER_OF_VALUES; i++)
displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
/* OR: if the array is easy to be hard-coded, then just hard-code it:
String[] displayedValues = {"100", "200", "300", .....}; */
Set arr in your picker :
numPicker.setMinValue(0);
numPicker.setMaxValue(displayedValues.size()-1);
numPicker.setDisplayedValues(displayedValues);
get/set the value of the picker :
//To get the current value in the picker
choosenValue = displayedValues[numPicker.getValue()];
//To set a new value (let's say 150)
for( int i=0; i<displayedValues.length ; i++ )
if( displayedValues[i].equals("300") )
numPicker.setValue(i);

Try the code below
int start = 100;
String[] numbers = new String[20];
for(int i =0 ; i < 20 ; i++) {
numbers[i] = start + "";
start = start + 100;
}
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.id_number_picker);
numberPicker.setMaxValue(20);
numberPicker.setMinValue(1);
numberPicker.setDisplayedValues(numbers);

final String[] nums = new String[21];
for(int i=0; i<nums.length; i++) {
nums[i] = Integer.toString((i+1)*100);
}
numberPicker.setMinValue(0);
numberPicker.setMinValue(19);
numberPicker.setDisplayedValues(nums);

Related

How to pass edit text values to integer array in Android

I'm a beginner in Android.
I have created a edit text field where can I enter values from 0-9. Now, I have to get these values in an integer array.
example : entered values are 12345.
I need an array containing these values
int[] array = {1,2,3,4,5};
I need to know the way to do this. Kindly help.
Try this :
String str = edittext.getText.toString();
int length = str.length();
int[] arr = new int[length];
for(int i=0;i<length;i++) {
arr[i] = Character.getNumericValue(str.charAt(i));
}
You can use something like this:
int[] array = new int[yourString.length()];
for (int i = 0; i < yourString.length(); i++){
array[i] = Character.getNumericValue(yourString.charAt(i));
}

Compare a value with all the values in arraylist in java

In my case Arraylist contains user define timings like{12:23,15:40,17:17...}.
how can i print message when system time is equal to user timings.
First make an array for this by below code
String s = "{12:23,15:40,17:17...}";
String newstring = s.replace("{").replace("}");
int size = StringUtils.countOccurrencesOf(newstring , ",");
String[] arrayString = new String[size];
Now Compare
for(int i=0; i<size; i++){
if(yoursystemtime.equals(newstring.split(",")[i])){
Toast.makeText(context, "equal", Toast.Lenght.Short).show();
}
}

how to set spinner item position

I am creating an android app in which I want to add values in spinner i.e countries. I added them successfully, but I want to show my desire country at the top of the spinner list i.e I want to show Australia at the 0 index of the spinner. So how I can achieve this? Anyone please help me.
Here is my code:
MySpinnerAdapter adapter = new MySpinnerAdapter(SignUp.this, android.R.layout.simple_spinner_item, countryname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_country.setAdapter(adapter);
spinner_country.setSelection(countryname.size() - 1);
spinner_country.setSelection(12);
Collections.sort(countryname);
Now I am doing this and it shows ArrayIndexOutOfBoundsException
String[] str = new String[countryname.size()-1];
str = countryname.toArray(str);
int indexTarget = 13;
String valueAtIndex = str[indexTarget];
for(int i = indexTarget; i > 0; i--){
str[i] = str[i-1];
}
str[0] = valueAtIndex;
for (int i = 0; i < str.length; i++)
{
System.out.println(str[i]);
}
List<String> stringList = new ArrayList<String>(Arrays.asList(str));
MySpinnerAdapter adapter = new MySpinnerAdapter(SignUp.this, android.R.layout.simple_spinner_item, stringList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_country.setAdapter(adapter);
spinner_country.setSelection(countryname.size() - 1);
Collections.sort(countryname);
If you want Australia to be at the first position in spinner then add it to zeroth index of your countryname.
You can start adding names to your countryname with Australia so that it will be at zeroth index and appear at first position in spinner.
spinner.setSelection(12) will select the 13th value from the array you are supplying to your adapter. It will not be at the first position.
EDIT
This is just an example, you need to modify accordingly. Here the original array is B, M, T, A, C, I want A to be first, so I need index of A. For your question you need the index of Australia
String[] str = {"B", "M", "T", "A", "C"};;
int indexTarget = 3;
String valueAtIndex = str[indexTarget];
for(int i = indexTarget; i > 0; i--){
str[i] = str[i-1];
}
str[0] = valueAtIndex;
for (int i = 0; i < str.length; i++)
System.out.println(str[i]);
Output
A
B
M
T
C
Now A is at first position.

Android shuffle Questions and Answers (string arrays)

I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}

Array integer Android

ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.

Categories

Resources