Dynamic variables in a for loop? - java

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);

Related

Edit text input into an array

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.

Change Layout position from code

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

How to link many TextViews with their resources

I would like to connect many TextViews with resources, for example, I have got 21 TextViews in xml file which all create a table. I would like to simple attach them to my code, but I want to avoid 21 lines of code.. I thought about looping it, but I am probably wrong, because it doesn't work.
for (int id = 1; id < 21; id++)
textView[id] = (TextView) findViewById(R.id.textView+"id");
}
You can link ViewGroup which is your table here and iterate their childs.
Lets assume we have a linear layout named as mLinearLayout.
for(i=0; mLinearLayout.getChilds.size; i++){
View child = mLinearLayout.getChildAt(i);
if(child instanceof TextView)
mList.add((TextView) child)
}
For more complex layouts you might need recursive functions to iterate child view groups.
Good luck
Instead of findViewById you can try findViewWithTag because now you can use a string argument like what you want.
Of course, you'll probably have to adjust a few things to be able to use this.
Your TextViews will need to be a direct child of a parent layout in your xml and tag them: android:tag="mytext1"
Then in Java, for example:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent);
...
textview[i] = (TextView) parentLayout.findViewWithTag("mytext" + i);
And you can loop that findViewWithTag

unable to get text of other textview in android?

I am having a dynamic table in which I have created lots of textview dynamically... actually I having one row in which I have 3 textview... in this one of them contains unique id but other's textview value repeats around the table... therefore on the click of other textview I need text from unique textview.... so please suggest some thing like getting text from the neighbor textview... line through which I am able to get text from the textview is following.
((TextView)v).getText().toString();
When creating dynamic TextViews assign a reference to the unique TextView as a tag using setTag to each of them.
TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTextView);
You can later get the reference back using getTag
public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();
}

Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.

Categories

Resources