Android Spinner adapting an array with unknown elements - android

I am building spinners which contain information which I am parsing from a certain document. So I decided to adapt the spinner to an array. If I were to initialize a string array like this:
String [] TEST = {"one", "two", "three"} and pass this string here:
ArrayAdapter<String> numAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, TEST);
Everything works fine. BUT, since I do not necessarily know what will go into the spinner I have a parser (which correctly parses and stores the information in the string array) which causes the program to crash when I pass the array to the ArrayAdapter. I tested it by building a simple string array with a loop like this:
for(int i = 0; i < 6; i++){
test[i] = {"hi"};
}
and it DOESN'T WORK. Any ideas why this might be?

No curley braces while assigning value, as well as you need to initiate array with size before using it. Here is example code. But this code will populate "hi" 6 times.
String[] test = new String[6];
for(int i = 0; i < 6; i++){
test[i] = "hi";
}

Related

Array, with if statement

So i am busy with a program where i have this array called names and in the array i have the following data:
public static String [] name = {"Products", "Customers","Calculators","Logout"};
public static String [] subName = {"140 wide Plain", "140 wide Pattern", "280 wide Running width", "280 wide plain drops","280 wide pattern drops"};
now the following code i am trying to implement an if statement:
private List<TitleMenu> getList() {
List<TitleMenu> list = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
List<SubTitle> subTitles = new ArrayList<>();
if (names.equals("Calculators")){
for (int j = 0; j < subNames.length; j++) {
SubTitle subTitle = new SubTitle(subNames[j]);
subTitles.add(subTitle);
}}
TitleMenu model = new TitleMenu(names[i], subTitles, null);
list.add(model);
}
return list;
}
so i only want the second portion of my code to implement if the if statement is correct but i cant seem to get the if statement correct. I am coding in android studio so i dont know how my if statement needs to be
You need to reference the indices of the array. Each index in an array is represented by a number that starts at base zero. This is an important and fundamental concept across many programming languages.
It also looks like your array's name is not names and is name.
So you could check to see if the name is "Calculators" by doing something like this:
if (name[i].equals("Calculators")) { ... }

Split strings on string array

i have String Array like this:
String[] q1={"AAA-BBB","AAA-CCC","AAA-DDD"}
and i want result like this
temp={"BBB","CCC","DDD"}
i tried below code but the result is wrong
for(int i=0;i<q1.length;i++){
ArrayList<String> temp=new ArrayList<>(Arrays.asList(q1[i].split("AAA-")));
}
Try like this:
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<q1.length;i++){
String[] array = q1[i].split("-");
temp.add(array[1]);
}
You could use substring:
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<q1.length; i++){
temp.add(q[i].substring(q[i].indexOf('-') + 1, q[i].length()))
}
you find error Because you use split
Splits this string around matches of the given regular expression.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
q1[i].split("AAA-")
in this line you got 2 result splited 0 = "" AND 1 = "BBB"
so you need to pick the sec result
you have multi Solution
like https://stackoverflow.com/a/50234408/6998825 said
String[] array = q1[i].split("-");
temp.add(array[1]);
//change this q1[i].split("AAA-") to
q1[0].substring(4)
if your AAA- is not going to change
Have you tried creating the ArrayList outside of the loop? As previously you were creating a new ArrayList for every element in your string array
ArrayList<String> temp = new ArrayList<>();
for(int i=0;i<q1.length;i++){
temp.add(q1[i].substring(4);
}
Assuming that "AAA-" is not going to change.

How can I put series of numbers to a list in android?

What I'm trying to is making a list which contains series of numbers like [1,2,3,4,5,6...100].
In Java, it is simple using 'range' so I tried to find similar class in android.
Therefore, I found that some classes like Range and Intstream, but I don't know how to use them.
I'll be appreciated if you teach me how can I get my purpose, thanks.
You could write a simple function which would look like this:
public List<Integer> buildList(int maximum) {
List<Integer> list = new ArrayList();
for (int i = 1; i <= maximum; i++) {
list.add(i);
}
return list;
}
And a call which produces your desired result would look like this:
List<Integer> list = buildList(100);
If you want an array instead of a list, do this:
int[] array = list.toArray(new int[list.size()]);
int size = 100;
ArrayList<Integer> list = new ArrayList<>(size);
for (int i = 1; i <= size; i++) {
list.add(i);
Log.i("Value is = ", i+"");
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
.....
You don't need to define the size because ArrayList is dynamically
while in some other language we use list[index] to get the value.
But here we use list.get(index) to do that.

how to use variable size array in android?

I'm using string array which I'm passing to ArrayAdapter for spinner item.
my array size(usually less than 10) and values are variable (I'm taking it from asset file)
if I'm using this :
String[] arr = new String[10];
protected void onCreate(Bundle savedInstanceState) {
breader = new BufferedReader(new InputStreamReader(getAssets().open(path)));
int length = Integer.parseInt(breader.readLine());
for(int i=0; i<length; i++){
arr[i]=breader.readLine();
initialize();
....
}
initialize(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> array = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, arr);
spinner1.setAdapter(array);
}
it shows NullPointerException at spinner1.setAdapter(array); line.
can I reassign array length. I think its not possible.
because of you add null when no data in last portion of array.
I mean there are 7 data in assest file and you move for loop 10 times so after 7 data Add there are null represent. so breader.readLine(); read null and add it to your string array and when you parse StringArray to Array Adapter there are null get on 8th position of array and nullpointer Exception fire.
So just check breader.readLine() != Null then add it to String of Array otherwise add some Temp text.
Or
You Also Use ArrayList instead of String[] Array.
ArrayList is dynamically Arraylist you can add data to list useing array.add(yourdata).
Thats it...

Converting an ArrayAdapter to a List<String>

I have an array adapter(string), and would like to convert it to a List<String>, but after a little googling and a few attempts, I am no closer to figuring out how to do this.
I have tried the following;
for(int i = 0; i < adapter./*what?*/; i++){
//get each item and add it to the list
}
but this doesn't work because there appears to be no adapter.length or adapter.size() method or variable.
I then tried this type of for loop
for (String s: adapter){
//add s to the list
}
but adapter can't be used in a foreach loop.
Then I did some googling for a method (in Arrays) that converts from an adapter to a list, but found nothing.
What is the best way to do this? Is it even possible?
for(int i = 0; i < adapter.getCount(); i++){
String str = (String)adapter.getItem(i);
}
Try this
// Note to the clown who attempted to edit this code.
// this is an input parameter to this code.
ArrayAdapter blammo;
List<String> kapow = new LinkedList<String>(); // ArrayList if you prefer.
for (int index = 0; index < blammo.getCount(); ++index)
{
String value = (String)blammo.getItem(index);
// Option 2: String value = (blammo.getItem(index)).toString();
kapow.add(value);
}
// kapow is a List<String> that contains each element in the blammo ArrayAdapter.
Use option 2 if the elements of the ArrayAdapter are not Strings.

Categories

Resources