Getting content from editText - android

In my application, i have a tableLayout with many editTexts in it. When i click "save"button, i want to access all the values entered in editTexts. I have assigned IDs in runtime while creating the table. Now how can i access the values from editTexts when "save" button is clicked...? I have used something like below to assign IDs,
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
...............
assignment.setId(i+j);
.............
}
Could anyone suggest a solution..?

Other nice solution is to cycle through all childrens of some Layout, so you don't need to set any special IDs. Just try:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();
for (int i=0; i<child_count; i++)
{
EditText text = (EditText) layout.getChildAt(i);
// do work with text
}
With some other code, you can do this for any other layout hierarchy.

How about something like:
ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
EditText text = (EditText)ActivityName.this.findViewById(i+j);
strings.add(text.getText().toString());
}
}
This would give you all of the values from all of the texts in one big array. If that's not what you want, let me know and I'll see if I can adjust the code.

Related

What's the best practice for keeping track of dynamically generated EditText?

I have a quantity of items which I'm retrieving from a database. The user is expected to enter a serial number for every item, and the input values are to be posted back to the database. Thus, a dynamic EditText is to be generated for every item. I normally do it this way:
int itemQuantity;
List<EditText> listEditText = new ArrayList<EditText>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for(int i = 0; i < itemQuantity); i++){
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
listEditText.append(editText);
}
Then I use listEditText.get(index).getText().toString(); to retrieve values from each EditText.
Is this the right way of doing when considering performance and good practices?

How to set the same property (invisibility) to several elements with a loop?

I have several elements that i want to set with the same property. Is it possible with a for loop? Thanks in advance. This is what I have so far:
public void invisible(){
int[] buttons = {R.id.key1, R.id.key2, R.id.key3, R.id.key4, R.id.key5, R.id.key6, R.id.key7
, R.id.key8, R.id.key9, R.id.key10, R.id.learn,R.id.kit1,R.id.kit2};
for(int i=0; i<buttons.length; i++){
//set invisible?
}
}
for (int i = 0; i < buttons.length; i++) {
findViewById(buttons[i]).setVisibility(View.INVISIBLE);
}
This should work. You iterate over the view ids, search for the view in your layout hierachy and set the visibility.

How to use dynamic textview from json in android

I am using post method to get this array and number of columns will increase based on every post.
"floor":
[
{"no_of_bedroom":"1.5","floor_plotsize_start":"692.00","floor_price_start":"4356832.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1000.00","floor_price_start":"6296000.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1029.00","floor_price_start":"6478584.00"},
{"no_of_bedroom":"2.0","floor_plotsize_start":"1132.00","floor_price_start":"7127072.00"},
{"no_of_bedroom":"3.0","floor_plotsize_start":"1390.00","floor_price_start":"8751440.00"},
{"no_of_bedroom":"3.0","floor_plotsize_start":"4901.00","floor_price_start":"40801320.00"}
]
How to display by using dynamic textview in android?
JSONArray jsonarray;
jsonarray = jsonobject.getJSONArray("floor");
{
//How to proceed by using dynamic textview?
}
Thanks in advance
I guess you can create a listview with textview of the item that you would like to display
My suggestion would be using a loop to get per object and place it inside the listview
peseudo code:
for(int i=0; i< jsonarray.size(); i++){
list.add(jsonarray.getJSONObject(i).getString("no_of_bedroom")); //just an example to check the details of no_of_bedroom key and add it inside the
}
//using list adapter HERE to display the item the TextView
It would be good for you to try it out and update us with more information of how you would like to do it.
You can dynamically create the textviews you want,then you can add them to your main layout and display. Below is a small hint for it:
JSONArray jArray = jsonobject.getJSONArray("floor");
for(int i=0; i < jArray.length(); i++)
{
TextView textview = new TextView();
textview.setText(""); //your own text
textview.setTextColor(Color.parseColor("#000000")); //your own color
//And now add this textview to your main layout
yourlayout.addView(textview);
}
try this
JSONArray jArray = jsonobject.getJSONArray("floor");
for(int i=0; i < jArray.length(); i++)
{
TextView textview = new TextView(this);
textview.setText(""); //your own text
layoutname.addView(textview);
}

Android: adding multiple Views programmatically

I want to add a LinearLayout wrapped around a TextView and Button programmatically. I want it to take a String array and then using the length of the string array, add that many TextViews each with their own button.
So first:
String [] s = { .... the values ....}
int sL = s.length;
TextView t1 = new TextView (this);
// then somehow create t2, t3... etc. matching the length of the String array.
Is this the best way to do this or is there another way to do this? For some context, it's a quiz app and I've created a list of categories inside resources as values and I'm trying to programmatically get my app to create as many TextViews as there are categories then set each TextView to each category then get each button to take the user to that category of questions.
You are starting it right, just do a for loop and add textviews to your linearlayout.
// You linearlayout in which you want your textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
String [] s = { .... the values ....}
int sL = s.length;
TextView textView = null;
// For later use if you'd like
ArrayList<TextView> tViews = new ArrayList<TextView>();
for (int i = 0; i < sL; i++)
{
textView = new TextView(this);
textView.setText(s[i]);
linearLayout.addView(textView);
tViews.add(textView);
}
There is nothing wrong with this way of doing it. If you want to use these textview later on (set text for them or something) store them in an Array of some kind. Edited code
You can do the following:
for(int i=0;i<s.length;i++){
TextView t=new TextView(this);
t.setText(s[i]);
yourLinearLayout.addView(t);
}
But I really think that using a ListView would be better for performance ;)

how to store multiple dynamic edittext value in android

I have multiple dynamic editTextBox. I want to know how can I store all its edit textbox value?
what should I write in all edit box that should be I want a store on button click event.
My code is below:
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1 = new EditText(this);
editText1.setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1);
tbl.addView(tr1);
}
Here is the code.
EditText[] editText1 = new EditText[6];
for(i = 0 ;i < 6; i++) {
TableRow tr = new TableRow(this);
TableRow tr1 = new TableRow(this);
TextView tv1 = new TextView(this);
editText1[i] = new EditText(this);
editText1[i].setWidth(300);
tr.addView(tv1);
tbl.addView(tr);
tr1.addView(editText1[i]);
tbl.addView(tr1);
}
public void onClick(View v)
{
for(int i=0;i<6;i++)
{
Log.i("----value of EditText:"+i,"."+editText1[i].getText().toString());
}
}
A direct answer to your question would be:
remember your textviews when you create them (add them to an ArrayList for example)
the onPause() method of your activity is the place to retain any information you want available later (android framework can finish() your activity anytime when it's paused).
in an overrided onPause() method, cycle through your ArrayList<TextView>, retrieve each TextView content and store them for later use. (If you have a fixed number of TextViews, you may consider using SharedPreferences, but take a look at the remainder of this article to see if something better suits your needs.
You can then use onCreate() or onResume() method to repopulate your TextViews before your application shows up.
An indirect, possibly better, answer would be to separate data from UI by using a data management class that would handle saving restoring and modifying your data and a customized Adapter which would be responsible of filling the TextViews. This way you can easily change the later to address different widgets.
I wrote these codes without using editor, check it before using
String sValues="";
for (int i = 0; i < tbl.getChildCount(); i++) {
TableRow row = (TableRow)tbl.getChildAt(i);
for (int j = 0; j < row.getChildCount(); j++) {
if(row.getChildAt(j) instanceof EditText){
EditText editText1 = (EditText)row.getChildAt(j);
sValues += editText1.getText().toString();
}
}
}

Categories

Resources