Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
TextView[] t = new TextView[8];
t[0] = (EditText)findViewById(R.id.seller1);
t[1] = (EditText)findViewById(R.id.seller2);
t[2] = (EditText)findViewById(R.id.seller3);
t[3] = (EditText)findViewById(R.id.seller4);
t[4] = (EditText)findViewById(R.id.seller5);
t[5] = (EditText)findViewById(R.id.seller6);
t[6] = (EditText)findViewById(R.id.seller7);
t[7] = (EditText)findViewById(R.id.seller8);

And Go On

What I have tried:

t[i] = (EditText)findViewById(R.id.selleri);
repeating many times in my code so i am trying to replace seller1,seller2.... by some sort of array of selleri. So i have try
Java
String[] Seller = {"seller1","seller2","seller3","seller4"};
and replace it in
Java
t[i] = (EditText)findViewById(R.id.seller[i]);
but gives error on seller[i].
Posted
Updated 5-May-23 22:23pm
v2

As you can see from your calls to findViewById, you cannot use strings to reference resources, you must use the actual resource identifiers. So you could try something like:
Java
int[] Sellers = {r.id.seller1, r.id.seller2, r.id.seller3, r.id.seller4, ...};
// and replace it in 
t[i] = (EditText) findViewById(Sellers[i]);
But all of this begs the question: do you really need EditText types for this, or could a List<T> work better?
 
Share this answer
 
v2
Comments
Vivek Kansal 24-Sep-20 12:47pm    
Not working sir. How List<t> can replace EditText? Is there any example?
Richard MacCutchan 24-Sep-20 16:50pm    
I have no idea, you need to think about what you are trying to do and whether your design is the best way of doing it. Go to the Android developer website and look at the documentation, it contains all the information you need.
Sandeep Mewara 24-Sep-20 17:14pm    
5! Above suggested code snippet makes sense and should work (array with actual IDs)
If you have fixed naming convention then code like below would be easier instead of defining an array with all IDs.

Try:
Java
ArrayList<EditText> myList = new ArrayList<EditText>();

// Removed array
String sellerID;
int resourceViewID; 

for (int i = 0; i < 4; i++)
{
   sellerID = "seller" + i;
   resourceViewID = getResources().getIdentifier(sellerID, "id", getPackageName());
   myList.add((EditText)findViewById(resourceViewID);
}

Reference: Java ArrayList[^]
Quote:
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
 
Share this answer
 
v3
Comments
Richard MacCutchan 24-Sep-20 16:56pm    
"// All viewIds here - can be any ID"
Yes, but they are not Strings.
Sandeep Mewara 24-Sep-20 17:12pm    
Thanks. Yes, I updated with the change. We can find that ID using string and use it. :thumbsup:
Richard MacCutchan 24-Sep-20 17:20pm    
Something new I learnt today. +5.
Abisay 16-Oct-23 17:11pm    
How do we call the data? With mylist?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900