Switching values in spinner based on values in ArrayList - java

I have an Array list which is generated dynamically, it has a capacity to store 5 elements in all. Now this arraylist can contain 2,3,4,5,6 as elements at all. Sometimes some of these elements might be missing as it is generated dynamically. I want to switch positions in the Spinner based in the array list on a button click. How do I keep track of which element is displayed and move to next element?
For example in onCreate I detect the first element of the Arraylist if that is 2 I set
spin.setSelectedItem(aList.get(1));
The above sete my selection to 2 in the spinner.
on Button Click I want to switch to the next element in the list - The problem here is that the next element can be present or not I use the following check:
if ((spin.getSelectedItemPosition()) < Integer.valueOf(pages.get(pages.size()-1)))
But now how set Spinner value to the next element in the above if condition?
EDIT
Currently I am doing something like:
int current = spin.getSelectedItemPosition();
int nextPos = (pages.indexOf(String.valueOf(current)))+1;
spin.setSelection(Integer.valueOf(pages.get(nextPos)));
where spin is my spinner and pages is the arraylist I have.

You can simply use current index and use setSelection method like this:
public void showNext(View v){
int nextIndex = sp.getSelectedItemPosition()+1;
if(nextIndex < spItems.size())
{
sp.setSelection(nextIndex);
}
}
If you don't want to use getSelectedItemPosition directly, you can keep a global integer variable, currentIndex, and set it in onItemSelected method of spinner. And use that in button click method.
Hope it helps.

Related

How can I get and set a string resource dynamically in a for-loop using a findViewById() method?

In my app, I want a counter from 0 to 8 to decide the number of players in a game.
Below there are 8 possible fields to write a name inside, which are all set to invisible. If the players-counter is set to 3 players, there should be the first 3 fields visible. Depending on the actual number of the counter, the visibility of the fields changes (1player = first field, 5 players = first 5 fields).
When the +1 (player) button is clicked, a certain method is activated. I tried to run a for-loop everytime the button is clicked. In this for-loop from 0 to "whatever amount" (max. 8 players) the actual fields should be found with "findById" and set to visible.
I tried it with a string resource (.xml) and I can get the text of the resource but with my thought process, I have to update the string resource to every number of the field (if 3 players: "field_" + "1", "field_" + "2", "field_" + "3").
How can I get and (most importantly) set/update a string resource for this specific purpose?
(Switch is too inefficient and I can't use a string with the findViewBy Id()-method by updating the String (not string resource) like mentioned before.
Please help, and accept the fact that I'm new to Android Studio for one week!)
You can use "getIdentifier" which takes a String parameter. So you can set the type as "id" in the second parameter of this method. This method returns the id of the view you want, but beware, it will throw a "FATAL EXCEPTION" if the id of the View doesn't exist. With this id, you can use findViewById to fetch the TextView and change its visibility. The "getIdentifier" method can be called from the "getResources()" method.
Below you can see what it would be like to make visible a TextView that has the id "textView1":
int id = getResources().getIdentifier("textView1", "id", getPackageName());
TextView textView = findViewById(id);
textView.setVisibility(View.VISIBLE);
Below you can see how you would make 8 TextView with id 1 to 8 visible:
TextView textView;
for (int i = 1; i <= 8; i++) {
int id = getResources().getIdentifier("textView" + i, "id", getPackageName());
textView = findViewById(id);
textView.setVisibility(View.VISIBLE);
}
So, just put the limit at i <= x , with x being the limit of players who will play:
TextView textView;
for (int i = 1; i <= totalPlayers; i++) {
int id = getResources().getIdentifier("textView" + i, "id", getPackageName());
textView = findViewById(id);
textView.setVisibility(View.VISIBLE);
}
Do you just want to make some EditTexts visible and others not? Personally I'd keep it simple, do the lookups once (in onCreate or wherever) and store the references in a list. Then when you need to display n fields, you can just iterate over the list and set the first n to VISIBLE and the rest to INVISIBLE.
I feel like it's fine to just list all the EditText IDs (R.id.field_1 etc) and generate your list of actual Views from that, but if that repetition bothers you, there's a few things you could do. Like:
set a tag attribute on each field in the XML, and use findViewWithTag to look them up, generating the lookup strings programmatically, like "field_" + i
do a similar thing with the resource ID, like in #Moises's answer
lookup their containing layout, use getChildCount and [getChildAt] to iterate over the views in that layout, and use isInstance to collect all the EditTexts in order3
create and add the EditTexts in code - you probably don't want to do this, but you could!
I'm not really sure what you mean about the string resource or what you're trying to do - I'd honestly just make a list of R.id.field_1 etc, iterate over that to do findViewById on each and store those in a new list, and you're done. Also my Java's a bit rusty so sorry no example code!

Loop in arraylist

This is my ArrayList:
{10, 20, 30,40, 50, 60, 70, 80, 90, 100....1000}
I want to loop my result on button click.
I want first 5 results in first Button's click.
Then automatically next 5 result likewise.
Is there any way I can do that?
for(int i=0 ;i<5; i++) {
list = array.getJSONObject(i);
fr.add(list.get("contact"));
}
The problem is that I am only getting first 5 not next results.
Thanks for helping.
If you want to remove first N items in an array list every time you call a method from index a to index b,
You can use ArrayList.removeRange(int fromIndex, int toIndex) every time you press that button (But don't iterate this )
or easily use ArrayList.subList(start, end).clear(); for the same task!
Removes from this list all of the elements whose index is between
fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding
elements to the left (reduces their index). This call shortens the
list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this
operation has no effect.)
Read document > http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#removeRange%28int,%20int%29
example :
public void removeFistNItemsFromList(){
for(int i=0 ;i<n; i++) {
// add your items or do what ever you want!
}
myArray.subList(0, 4).clear(); // here from 0 to 4th index items will be removed and list will be updated!
System.out.println("MyArrayListNow:"+myArray);
}
output after each click :
Well, you are trying to reading first 5 elements always hence the problem. One option you have read and remove the elements from the arraylist. But a better approach would be to keep a count of mouse click, in which case it is a classic pagination problem, and u can fetch the next 5 elements and you will not lose the data in arraylist.

How to Iterate over entire android listview

I have a listview where user choose how much for each item he wants, so I need to iterate over each row to get the name (Ovo, Presunto, Queijo, etc), the quantity (the number between the "+" and "-" buttons) and the price. This is already working fine, but I have the following problem: If you look at the bottom of the listview you can know that there are more items, in this picture there are 9 items, but there are 15, and if the user scrolls I will can not iterate over the "hidden" items of listview.
Today I have this code, where I try iterate over the elements of listview:
for (int i = 0; i < listView.getCount(); i++) {
v = listView.getChildAt(i);
if(v != null){
value = (TextView) v.findViewById(R.id.edOptionValue);
nome = (TextView) v.findViewById(R.id.tvOptionName);
tvPrice = (TextView) v.findViewById(R.id.tvPrice);
String price = tvPrice.getText().toString();
//more code here
}
}
listView.getCount() gets the correct number of items (15 items), but when i==10 listView.getChildAt(i) is getting null.
Is there some way to iterate over all items of a listview, incluing the "hidden" elements ?
You should not iterate the list views, instead you should keep all that data (price/quantity) in a list and iterate over that if you really need to do that. That list can be used to populate the ListView as well.

check if array is full

I am trying to do check if an array is full and print out to the user he is unable to enter more books.
static Object[][] books=new Object[2][];
I am asking 3 values from the user and am storing to another array called "row".
Object[]row=new Object[3];
After that i loop through the books array and check if it has a null value inside of it and add the "row" array with the given values of the user.
the problem am having is i cant give any feedback if books array is full after entering two rows of values.
boolean empty=false;
while(empty==false){
for (int i = 0; i < books.length; i++) {
if(books[i]==null){
books[i]=row;
empty=true;
break;
}
}
}
Why not having a variable
int bookCounter = 0;
which you can increase/decrease everytime you add/delete books and then just check it against your max number of books (which I assume is books.length)
In this way you don't need to loop over the array, which is not efficient.
There is no need for the while loop. After the for loop ends, check your empty variable that you're already setting. If it's false, then give your feedback to the user.
Arrays are always full. Even each element is null. If you initialize it with the size of 10, then JVM will alocate memory and fill the all the arrays positions with default value. What is: 0 for primitives numbers and char type, false for primitive boolean type and null for Objects.
So, your code won't work with a int[] for instance. Because there won't exist any null element.
That's why you dont have a count() method... You can create a method that is named countNotNull(). But you'll need to interate for all the array long.
The best solution is to use a variable to count when you add/remove itens form array. As
Guillermo Merino said.

Remove List Items and displace the others

I have a List of Buttons and I want to delete the first and the second item. Can i move the rest of Buttons in the List so that the third Button is now the first, the 4th the second and so on?
for(int i = 0; i<nleer; i++){
LinearLayout temp = layoutList.get(letzte);
int tempid = nextButtons.get(i).getId();
CharSequence temptext = nextButtons.get(i).getText();
leerButtons.add(new Button(this));
leerButtons.get(i).setOnClickListener(this);
leerButtons.get(i).setEnabled(true);
leerButtons.get(i).setId(tempid);
leerButtons.get(i).setText(temptext);
leerButtons.get(i).setLayoutParams(new LayoutParams(myButtons.get(i).getWidth(), LayoutParams.WRAP_CONTENT));
tempor.addView(leerButtons.get(i));
nextButtons.remove(i);
at the end i remove the Items (often i = 2) and after this loop i want to use the 3rd Item of the List but i don't know how to move them.
If you have a List x, and you do x.remove(0) to remove the first item, the index of the first item remains 0 (all items shift forward one position). It sounds like you're asking for the default list class behavior.

Categories