I want to work dynamically therefore I want to bind text views dynamically I think an example would explain me the best
assuming I want to bind 7 image views i can do it like this :
Country = (EditText)findViewById(R.id.CountryEditText);
City = (EditText)findViewById(R.id.CityEditText);
LivinigCreture = (EditText)findViewById(R.id.LivingCretureE);
Nature =(EditText)findViewById(R.id.NatureEditText);
Inanimate = (EditText)findViewById(R.id.InanimateEditText);
KnowenPersonality = (EditText)findViewById(R.id.KnowenPersonalityEditText);
Occupation = (EditText)findViewById(R.id.OccupationEditText);
but lets change 7 with NUMOFFILEDS as a final where i want to do the previous ?
myImages = new ImageView [7];
for (int i = 0; i<7;i++,????)
myImages[i] = (ImageView)findViewById(R.id.initialImageView01);
notice : in my R file the R.id.initialImageView01 - R.id.initialImageView07 are not generate in a cont gap between them therefore I don't know how to make this architecture possible .
and if there's a way can someone show me an example how to work dynmiclly (like using jsp on android combined way or something ?)
id its possiable to do so constant times is it possible to build an the same xml constant num of times like jsp does
thank u pep:)
You can store the IDs themselves in an array at the beginning of your Activity; that way you'll only need to write them once and you can index them afterwards.
Something like:
int[] initialImageViewIds = {
R.id.CountryEditText,
R.id.CityEditText,
R.id.LivingCretureE,
R.id.NatureEditText,
R.id.InanimateEditText,
R.id.KnowenPersonalityEditText,
R.id.OccupationEditText
};
Then you can access them with:
myImages = new ImageView [7];
for (int i = 0; i<7;i++) {
myImages[i] = (ImageView)findViewById(initialImageViewIds[i]);
}
If that's not enough and you really want to get the IDs dynamically, I suppose you can use reflection on the R.id class, possibly with something like R.id.getClass().getFields() and iterate on the fields to check if their names interest you. Check reference for the Class class, too.
Related
I'm working on the automation script, where I'm trying to click on the element based on content-desc, The code I tried is
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains("Subway Surfers")')
app_element.click()
This works for me
But I want to know is it possible to store the 'Subway Surfers' in a string variable and assign it inside the descriptionContains("Subway Surfers")') like
content_description = 'Subway Surfers'
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains(content_description)')
Like this is it possible can any one help me on this
Try this:
content_description = 'Subway Surfers'
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains('+content_description+')')
So far I have this without crashing.
String logoImage = JO.getString("logo");
if (JO.getString("logo") == "able")
{
channel.ivLogoPic.setImageResource(R.drawable.able);
}
The problem is, It's not changing the image.
I've tried the .setDrawable and that didn't work either.
String logoImage = JO.getString("logo");
if (JO.getString("logo") == "able")
{
//int drawable1 = R.drawable.abc;
channel.ivLogoPic.setDrawable(R.drawable.able);
}
Though [ #sunil sunny ] has already mentioned a helping link, I would also like to add some solution here that could be handy.
Remember, the efficiency of this solution, in cases like yours, will decrease with increasing number of drawable resources. Meaning, do this if there are not many drawables to use.
What you can do is first get the drawables as global variables in the recyclerview adapter:
int drawable1 = R.drawable.drawable1, drawable2 = R.drawable.drawable2 ... etc.
Then inside onBindViewHolder() simply use the resources depending upon what is obtained from the JSON response.
Like:
if(logo == 1)
{
logoImageView.setDrawable(drawable1);
}
One advantage to note of this is you can change drawables easily, and not have to resort to changing names in both resource file as well as the API. Meaning, decouples the two.
PS: The codes written here are for reference only.
i figured it out in my backgroundTask.Java file I have
while (count<jsonArray.length())
{
JSONObject JO = jsonArray.getJSONObject(count);
count++;
Channel channel = new Channel(JO.getString("name"),JO.getString("logo"));
Channel logoImage = new Channel(JO.getString("logo"));
if (logoImage.equals("axle"))
{
channel.ivLogoPic.setImageResource(R.drawable.axle);
Then I had to set the change in the Adapter
if(channel.getLogo().equals("axle")){
holder.ivLogoPic.setImageResource(R.drawable.axle);
}
I am making an app with Xamarin Android. It has 4 turns, and I want a random picture+sound to display in every turn. So far so good, but now I want to make it in a way that it won't show the same picture+sound(the same random number) twice. I have tried some codes from other treads, but unfortunately that did not work.
Following is what I tried from the other treads:
if (ActivityGame.playing == false)
{
List<int> alreadyGuessed = new List<int>();
Random randomSound = new Random();
int theSound = randomSound.Next(1, 5);
while (alreadyGuessed.Contains(theSound))
theSound = randomSound.Next(1, 5);
alreadyGuessed.Add(theSound);
return theSound;
}
You declare the alreadyGuessed list inside the if-branch. So, every time you get in that if branch, the program creates a new, empty list named alreadyGuessed.
You should make alreadyGuessed a member variable of your ActivityGame class, so it will retain the values already used.
I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);
I have many buttons in my activity (only a subset of which are visible at a time). I currently have something ugly like this:
buttonID[0] = R.id.buttonr1b1;
buttonID[1] = R.id.buttonr1b2;
buttonID[2] = R.id.buttonr1b3;
buttonID[3] = R.id.buttonr1b4;
...
buttonID[35] = R.id.buttonr1b36;
for (int i = 0; i < 36; i++) {
button[i] = (Button) findViewById(buttonID[i]);
}
Is there a more elegant way to reference all of R.id.buttonXXX ? It just looks so wrong and ugly.
Thank you.
Your instincts are correct. It's ugly and in general if you find yourself wanting to do this you should rethink your design.
If your buttons are uniform to the point where you want to loop over them to do something like this, they're probably uniform enough to generate programmatically in the first place (and you can store references as you create them) or use some form of AdapterView. What data needs to be associated with each button? Can you associate it directly using setTag/getTag? Do you need to use IDs here at all?
I'm not sure if this is more elegant or less elegant, because you will lose compile-time checking of your IDs. However, you can construct the IDs by name:
final static String PREFIX = "buttonr1b";
...
Resources res = getResources();
for (int i = 0; i < 36; i++) {
int resID = res.getIdentifier(PREFIX + i , "id", getPackageName());
button[i] = (Button) findViewById(resID);
}
Note: make sure "getPackageName()" would return the appropriate package for your R class, otherwise specify it explicitly.