I have a code adding multiple EditText. How can i store them into a array. This number 10 is just example, the number may bigger than that. How can i store it after click a button
for(int i=0; i<10; i++) {
String store[] = new String[10];
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
}
In your example the store variable isn't actually being used, did you intend do use it for storing the EditTexts?
Instead of using an array of String, just use an array of EditText and store a reference to them:
EditText store[] = new EditText[10];
for(int i=0; i<10; i++) {
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
store[i] = addAnsMCQ; //store a reference in the array to the EditText created
}
Then outside of the for loop, you can access the reference to each EditText, e.g.
store[0].setWidth(300);
You need to keep/get a reference to each of your EditText's then you can look up its value with .getText().toString() which you can store in whatever manner you like.
However if as you say
This number 10 is just example, the number may bigger than that.
If the number is going to be larger you should be using an Adapter and a ListView or something to hold your View objects. That will make it easier to get everything on to the screen. And will give you the benefit of view recycling.
Related
Currently I have added the dynamic TextInputEditText fields to the LinearLayout where all the dynamically added fields stores(only holds dynamic EditText fields only).
However, when I read each EditText field and fetch it's data, the value of last text field replace all the other values in the array.
Example:
Adds 3 dynamic fields, with the corresponding values of "AA","BB","CC". When i read the array, it shows like this,
Output: "CC,CC,CC"
Code:
private void fetchCertificates(){
ArrayList<String> certs = new ArrayList<>();
for(int i =0;i<linearLayout.getChildCount();i++){
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = findViewById(R.id.new_certs);
String name = newCerts.getText().toString();
certs.add(name);
}
String certList = android.text.TextUtils.join(",", certs);
Log.i("Certificates",certs);
}
Objective:
Read dynamically added TextInputEditText, and store the values in an array.
References: page-1 (This did not work)
I was able to resolve it in following way, thanks for the questioning hellboy and blackapps, it made me think bit differently.
private void fetchCertificates(){
ArrayList<String> certs = new ArrayList<>();
for(int i =0;i<linearLayout.getChildCount();i++){
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = certificateView.findViewById(R.id.new_certs);
String name = newCerts.getText().toString();
certs.add(name);
}
String certList = android.text.TextUtils.join(",", certs);
Log.i("Certificates",certs);
}
The problem in your code is at these lines,
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = findViewById(R.id.new_certs);
Assuming that the LinearLayout(parent) carries only TextInputEditText(children), when you iterate through LinearLayout you will get TextInputEditText only, so your code should be like,
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = (TextInputEditText) certificateView;
or
TextInputEditText newCerts = (TextInputEditText) linearLayout.getChildAt(i);
In your code since you directly used findViewById you always got the last to the latest view(child) with this id(remember if there are multiple views with the same id, then the latest view defined, both in Activity or layout, will be fetched) that's the reason you get CC, CC, CC.
I have the following code. I added my text fields dynamically. My desired result shown in Genymotion 5.0 (Google Nexus 5) but when I run my app in other devices/actual device the textfields get shuffled. Please help, Thanks in advance.
JSONObject jsonObject = new JSONObject(question.getSublabels());
final EditText[] editTextSublabels = new EditText[jsonObject.length()];
for (int i = 0; i < jsonObject.length(); i++) {
String names = jsonObject.names().get(i).toString();
editTextSublabels[i] = (EditText) LayoutInflater.from(activity).inflate(R.layout.sublabels, null);
editTextSublabels[i].setId(i);
editTextSublabels[i].setHint(jsonObject.getString(names));
sublabelsContainers.addView(editTextSublabels[i], params);
}
You cannot and should not rely on the ordering of elements within a JSON object.
In JSON, an object is defined thus:
An object is an unordered set of name/value pairs.
If you want order to be preserved, you need to redefine your data structure or put it inside a jsonarray
see http://www.json.org.
A JSONObject is a type of map. It does not preserve ordering. If you want to preserve ordering using JSON, you will need to use an array (and matching JSONArray in Java).
I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.
Simply I want to iterate ids by using pre-string values.
Psuedo example:
String pre_value = "textView";
for(int i = 0; i < size; i++) {
String usable_resource_id = pre_value + Integer.toString(pre_value);
// So how to use these id like R.id.textView1
// Cast or something similar
}
Any suggestions?
You can use Resources#getIdentifier() to get the identifier from a string.
But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?
In my app, I have a bunch of images in my drawable folder which I select at random and display using imageView. I've been told about ArrayList's which can add/remove objects from the list...in order to prevent image repeats, some sample code I used below:
// create an array list
ArrayList imageHolder = new ArrayList();
int remaining = 10;
public void initArrayList(){
// add elements to the array list
imageHolder.add((int)R.drawable.child0);
imageHolder.add((int)R.drawable.child1);
imageHolder.add((int)R.drawable.child2);
imageHolder.add((int)R.drawable.child3);
imageHolder.add((int)R.drawable.child4);
imageHolder.add((int)R.drawable.child5);
imageHolder.add((int)R.drawable.child6);
imageHolder.add((int)R.drawable.child7);
imageHolder.add((int)R.drawable.child8);
imageHolder.add((int)R.drawable.child9);
}
//get random number within the current range
int randInt = new Random().nextInt((remaining-1));
//update the imageView config
ImageView image = (ImageView) findViewById(R.id.shuffleImageView);
image.setImageResource(imageHolder.get(randInt));
Eclipse reports that image.setImageResource cannot use an object argument, which is what is provided by arrayList. The actual argument should be int. Any clue how to get around this??
Thanks in advance!
Use List<Integer> imageHolder = new ArrayList<Integer>();
ArrayList contains Objects, always, never primitive types. When you set ints into it, they are autoboxed to Integer objects, when you get them back, you get Integer objects as well. A short fix will be:
image.setImageResource((int)imageHolder.get(randInt));
Be careful though, unboxing a null pointer will cause a NullPointerException, So make sure your randInt is in the range of the arraylist.
EDIT:
I totally missed that, but You initialize your ArrayList like that:
ArrayList imageHolder = new ArrayList();
Which creates ArrayList of Objects. instead, initialize the ArrayList like the following to create ArrayList of integers:
List<Integer> imageHolder = new ArrayList<Integer>();
I have to create an app in android with a database.In that database I have a predefined list of products.
Now,the thing is that my ap has to offer to the user the posibility to introduce in that list some other products which are not in the list.
To this end, I've created an autocomplete text view in which I introduce a new product and I take the text fro autocomplete and I have to write it in the database
Now,my problem is that when I display the products that I've introduced in the database,the toast text that I use to display what I have in the database it shows me nothing next to "product......".
Now,that may be because when I try to get the text from the autocomplete I get nothing in return?
This is how I read from autocomplete:
mItem = (AutoCompleteTextView) findViewById(R.id.todo_edit_item);
String nou=mItem.getText().toString();
And then I compare nou(which is what I wrote in the autocomplete) with what I have predefnied in the list,so if it is a new product(which was not in the list already) the I add it in the database:
for(int i = 0; i < l; i++)
{
if (nou!=fruits[i])
t=true;
else t=false;
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}
Anyone any ideas of what I'm doing wrong in here cause I can't figure out.I'lll be here for further details.Thank u in advance:)
You compare strings using != instead of using !nou.equals(fruits[i]). also you compare to all elements in array each time, since you so t is always the value of the comparison to the last element in the array whether a match was found or not.
It should be written like that:
t = true;
for(int i = 0; i < l; i++)
{
if (nou.equals(fruits[i]))
{
t=false;
break;
}
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}