I have an array which contains some numbers:
int[] IDs = new int[] { 2, 5, 6, 11, 15};
There are many TextViews in Layout Resource:
TextView1;
TextView2;
TextView3;
TextView4;
TextView5;
TextView6;
and ... .
How Is it Possible to Access These in a for loop to (for example) change Their Text like this:
for(int i=0;i<IDs.length;i++){
TextView[i].setText = "something";
}
problem:Eclipse Doesn't Recognize "TextView[i]".
Note:I want to change the text of those TextViews which their name ends with one of the numbers in the IDs array.
You should be doing this, this is assuming the IDs correspond to the id's of the text views, if not you have to create an array of the id's
int[] IDs = new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5};
TextView textView = (TextView) rootView.findViewById(IDs[i])
textView.setText = "something";
If it's an activity, then remove the rootView.
If you have a given Layout, you can iterate over the children in your layout.
Just put all TextViews in one Layout.
For example:
LinearLayout la = findViewById(R.id.myLayout);
for (int i = 0; i < IDs.length; i++){
if (la.getChildAt(i) instanceof TextView){
TextView tv = (TextView) la.getChildAt(i);
}
}
Edit:
It might be better to count the numbers of found TextViews, if you have other elements in your Layout. Otherwise it will not find all TextViews, if we only count to IDs.length.
Related
I have a set of textviews like this..
TextView d1,d2,d3,d4,d5,d6,d7,d8.....etc
How can I set text in a loop like this by increment the number after d like this
for(int i=1;i<=8;++i)
{
d+i.setText("foo");
}
You can do like below.
Create ArrayList<TextView> and add all TextView in list.
Then after you have to do loop with List size.
and you can update text to TextView.
With this:
for(int i = 1; i <= 8; i++) {
int id = getResources().getIdentifier("d" + i, "id", getPackageName());
TextView tv = (TextView) findViewById(id);
tv.setText("foo");
}
the method getIdentifier() gets the integer id of a View by its id "name".
If this code is not inside an activity, you must provide a valid Context for the methods getResources() and getPackageName(), like:
context.getResources() and context.getPackageName().
I have a spinner with the values = {4,5,6,7} , initially spinner selected value is set to 4 , therefore 4 TextViews and 4 EditTexts showing in the activity.
Now I want if the user select value 5 from the spinner then these previous 4 TextViews and EditTexts will overwrite with new 5 TextViews and 5 EditTexts.
How can I do so ? any help?
For this you can manage through loop like...
for( int i = 0; i < spinner.getSelectedItem(); i++)
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
For create view one after another create programmatically layout like this:
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL)
Like this you can manage programmatically as per your need.
You can write your TextView and EditText as an item of RecyclerView and every time you can have as much items as you want.
OR
every time add TextView and EditText programmatically from the java code.
Context: I am trying to make an application that allows me to have multiple edit texts (in a to do list format). I am using a linear layout with a scroll view inside of it so that I can allow users to have as many notes as they need.
Question: How can I put each editText inside an array, furthermore, how can I put the string contents of each of the editText in an array.
Any help would be much appreciated!
You need to get text from your EditText like this
EditText et = FindViewById<EditText>(Resource.Id.et_nnnn);
string a = et.Text;
And then add to your array
You can get all child views of parent layout in an array. use something as below. pass parent layout view to follow function
SparseArray<Edittext> array = new SparseArray<Edittext>();
.
.
.
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
To get all edittexts text you can loop over that array and store in different array or list using gettext on each child.
I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView
I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);