I have an 8x8 board with 64 four fields, each being a LinearLayout.
Each LinearLayout has its ID like (field1, field2... field64).
Now I want to modify the background of each field later in my program but I don't know how to retrieve the layout I want to modify. I wanted to put all of them in the Array but it's not working.
private LinearLayout[] fields = new LinearLayout[65];
this.fields[field_id].setBackgroundColor(colour);
Another approach:
for (int i=1; i<65; i++) {
findViewById(R.id.field+i).setBackgroundColor(Color.WHITE);
}
It does not work as well (obviously). How should I approach this?
You can add them to a list, like this:
List fieldList = new ArrayList(64);
fieldList.add(R.id.field1);
fieldList.add(R.id.field2);
fieldList.add(R.id.field3);
// etc.
fieldList.add(R.id.field64);
Or use any other method to create such a list/array. It is a bit tedious, adding all fields like this, but it provides a usable list that can be used in loops.
It is also possible (I think), to renumber the ids of the fields (in R.java). If you make them subsequent, you can use the method you tried in the example code of your question. I do not not for sure whether these IDs stay the same when R.java is re-generated... If not, this method is not usable!
Make an Array with your ids.
int[] ids = {R.id.field1,R.id.field2...}
for (int i=0; i<ids.lenght; i++) {
findViewById(ids[i]).setBackgroundColor(Color.WHITE);
}
OR
ViewGroup viewgroup = findViewById(R.id.parentview);
for(int i = 0; i < viewgroup.getChildCount();i++)
{
View child = viewgroup.getChildAt(i);
}
You can access them this way, a tiny bit slower but doubt you will have a problem with it:
for (int i=1; i<65; i++) {
int id = getResources().getIdentifier("field" + i, "id", getPackageName())
findViewById(id).setBackgroundColor(...);
}
Sounds like a lot of work adding them in xml(I assume?) like that though. Preferably you would add them by code and then just save them in an array.
Related
I want save an object multiple times, the code below can not work:
for(int i = 0; i < 5; i++) {
repository.save(object);
}
Then I change the source to:
List<SomeObject> objectList = new ArrayList<>();
for(int i = 0; i < 5; i++) {
objectList.add(object);
}
repository.save(object);
But it also can not work, It only save the object one time to database.
Maybe because it point to one instance, but I want to know if there is an easy way to save an object multiple times?
Thank you!
It saves only 1 time the object because you are using the same instance every times.
So the first time you use object, so now it has an ID, then you use object again, but as we said it has an id so instead to save it, yours program update it inside you db.
For example if you do this:
for(int i = 0; i < 5; i++) {
repository.save(new Object());
}
Or if you have a list of Objects, like
List<SomeObject> objectList = new ArrayList<>();
for(int i = 0; i < 5; i++) {
objectList.add(object);
}
you could do this:
repository.saveAll(objectList);
Now you find 5 rows inside your table
I am not familiar with Spring data and might misunderstand the problem but generally there is no point to save the same object many times.
You need to explicitly create a clone/copy (or so) of an instance and save all these instances separately if you need multiple instances in database.
For example add creating new instance in a loop that saves the object.
Using the constructor, you can save same object multiple time:
Repository.save(new object(String name,String age));
private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.
i wanted to know how i can iterate through a collection of classes and be able to use the classes functions on every pass.
Im new to java and im much more familier with c++.
This is the collection i wish to iterate..
private ArrayList<Album> albumCollection;
and activate the following function
get title
This is my current code...
//Lists all stored titles
private void ListAllTitles(){
int size = albumCollection.size();
for(int i=0; i < size; i++){
System.out.println(albumCollection(i).getTitle());
}
}
In order to access an element from a List, you need to use the get method:
System.out.println(albumCollection.get(i).getTitle());
Also note, you could use the for each loop to achieve this:
for (Album album : albumCollection) {
System.out.println(album.getTitle());
}
This for each construct is simpler:
for (Album album : albumCollection)
{
System.out.println(album.getTitle());
}
What I want is to create multiple view in for loop
example
for(int i =1; i<5; i++){
GridView view = new Gridview(this);
}
But It creates 5 gridview with the same name.. so in future i can't set different option to a specific gridview. How do I get, that gridivew created in a loop get view + i name
Use a List
List<GridView> views = new ArrayList<GridView>();
for(int i = 1; i < 5; i++) {
views.add(new GridView(this));
}
Then you can get your views with
views.get(i);
Also, in your example, when a step of the for loop ends, the reference to that object is lost as the scope in which they were created is left. With no reference to the objects, the Garbage Collector comes in and frees that memory, deleting the objects.
So, you won't be able to access not even the last object created. If you modify the code like this, at the end of this code you will have only the last object instantiated:
GridView view;
for(int i =1; i<5; i++){
view = new Gridview(this);
}
Now the object exists in the scope you are in at the end of the code snippet. But only one object really exists.
So, the solution is to store the objects in some additional structure: an array if you know precisely how many objects you want, or some self dynamically allocated collection structure. And you have examples of both in the other answers.
Added: What you are actually asking for (to dynamically build the object's reference name) is called metaprogramming. I don't know if it is possible in Java, but here is an example of this done in PHP:
class Object {
function hello(){
echo "Hello \n";
}
}
for($i =1; $i<5; $i++){
$name = "view".$i;
$$name = new Object();
}
$view1->hello();
$view2->hello();
$view4->hello();
Here is runnable code: http://codepad.org/bFqJggG0
Use a List of GridViews.
List<GridView> grids = new ArrayList<GridView>();
for(int i =1; i<5; i++){
grids.add(new Gridview(this));
}
I think this code will create 5 GridViews, 4 of which will become immediately available for Garbage Collection as your code no longer has a reference to them.
If you create them in a loop, then I think I'd be looking to store them in a data structure such as a List or Map and then accessing them via an index or key.
i think you can do something like this :
for(int i =1; i<5; i++){
GridView view = new Gridview(this);
view.setId(i);
}
and then , you can difference between all views
GridView[] view=new GridView[5];
for(int i=1;i<5;i++){
view[i]=new GridView(this);}
Now I think you can set specified options.
package net.example.view;
class StatusBar extends View { ... }
I use it in my layout as following:
<net.example.view.StatusBar .../>
I must find it by it's type(class), I mean something like:
myActivityInstance.findViewsByClass(StatusBar.class) // returns View[]
Is it possible? If not, what is the better approach to find an element without having to use findViewById()?
Thanks.
If you can't use the id and your content view is some kind of ViewGroup you can iterate over the children.
ViewGroup vg = (ViewGroup) activity.getContentView();
for(int i=0; i<vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
if(v instanceof StatusBar) {
// do something interesting.
}
}
Also if you know the specific index you can call getChildAt(index) directly.
I don't think activity has a method that will return an array of Views to you based on class type. You could create and add them to your layout dynamically if you want to get an array of them without using findViewById() something like this:
mLayout = (LinearLayout)findViewById(R.id.mLayout);
StatusBar[] mBars = new StatusBar[10];
for(int i = 0; i < mViews.length; i++){
mBars[i] = new StatusBar(yourActivity.this);
mLayout.addView(mBars[i]);
}
That would give you an array full of StatusBar objects that have all been added to your layout. (You could make the array of type View if you wanted also).
If you are wanting to define all of your views in XML I don't think there is a way that you can avoid using findViewById() in a loop similar to this to get an array full of references to all of them. For this method you'd probably have to create an array of ints that is the same size that contains all of the resIDs of your views
ids[0] = R.id.mStatusBar1;
ids[1] = R.id.mStatusBar2;
etc.