Am getting value here for Checkbox, and textview properly. But, I could not get the EditText Values. am getting empty only.
my sample code:
View view = null ;
for(int i = 0; i < list.getCount(); i++ ) {
view = list.getAdapter().getView(i, null, null) ;
TextView textViewNickName = (TextView) view.findViewById(R.id.textview);
String textviewValue = textViewNickName.getText().toString().trim() ;
Log.v("ppi", "textViewNickName::"+textviewValue);
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
CheckBox checkBoxOne = (CheckBox) view.findViewById(R.id.checkBox1) ;
Log.v("ppi", "checkBoxOne::"+checkBoxOne.isChecked());
}
I referred this link also :
Iterate through ListView and get EditText-Field values
Thanks Advance
You have mistake to get value from EditText, any event time you can get value initialization time you can't get text from EditText
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
see above code that in your mistake if you have no enter any string in EditText then how can you retrieve initialization time.
you have to make change in you code as like blow.
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
}
});
Related
I need the value of inflated editText, I am inflating the design layout which is the child layout and MainActivity is a parent layout.
below is the MainActivity class
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is total code of store object and retrieval,
read all comments please,
public class MainActivity extends AppCompatActivity {
View array[];
String text;
LinearLayout container;
// list of edittext
List<EditText> edtlist = new ArrayList()<EditText>;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext1 = (EditText) findViewById(R.id.email);
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
// store object in list
edtlist.add(edittext);
text = edittext.getText().toString();
}
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplication(), "" + text, Toast.LENGTH_SHORT).show();
}
});
// call this whenever you want in any event
// here 0 is first edittext
// you can get all text value using for loop
// list.get(0).getText().toString();
}
}
You are looking for something like ,I'm not sure about it maybe it works maybe it wont.! it just a rough concept based code havnt Implemented yet.!
List<EditText> edtxtList;
List<String> edtxtStrList;
for (int i = 0; i < 4; i++) {
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearlayout);
View v = getLayoutInflater().inflate(R.layout.design, myLayout, false);
myLayout.addView(v);
int id = View.generateViewId();
v.setId(id);
EditText edittext = (EditText) v.findViewById(R.id.edittext);
text = edittext.getText().toString();
edtxtList.add(edittext);// try something like this.!
}
Then use Foreach loop on EditText;
for (EditText edText : edtxtList) {
if(!edText.getText().trim()=null){
edtxtStrList.add(edText.getText().trim());
}
}
I havnt tried It yet but maybe this ruff concept may helps you.!
Maybe this Method helps You.!
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
In Above code mLayout is your layout.!
<LinearLayout android:id="#+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>
I used the answer to this question to get the basic value of the TextView clicks into a single TextView .This code gives a per click value, I want to know how to add the existing value of TextView.
Note: m1aa and m1ab are number variables in a previous activity and received through intent on this one.
UPDATE This question should have been "how do you convert a string to a decimal."
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***UPDATE***WORKS FINE :)
AFTER GETTING HELP AND "THINKING" ABOUT WHAT I WAS WRITING I END UP WITH...
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);
You can use following code to get a value of TextView and convert it to integer
String s = textView.getText().toString();
int n = Integer.parseInt(s);
Remember NumberFormatException will be thrown if the string does not contain a parsable integer.
Is this code completely correct or not?
myRLayout = (RelativeLayout) findViewById( R.id.rel_layout );
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
TextView res = (TextView) findViewById(R.id.result_text);
String str1 = ((EditText) findViewById(R.id.edittext_1)).getText().toString();
String str2 = ((EditText) findViewById(R.id.edittext_2)).getText().toString();
res.setText(str1+str2);
}
}
Why I ask? For each iteration only one EditText gets in action or all together at once?
use v instance of Child to get value from all EditText and use TextView.append for showing values in TextView. try it as:
TextView res = (TextView) findViewById(R.id.result_text);
res.setText(""); //<< clear textview here
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
String str1 = v.getText().toString(); //<< get text from ith EditText
res.append(str1); // append value to result_text TextView
}
}
I am adding programatically and dynamically some elements (buttons and text views) with android. I also need to set the setOnClickListener event for each of these buttons and from that event execute some action on the click of button:
do
{
EditText txt1 = new EditText(this);
EditText txt2 = new EditText(this);
Button showtxt = new Button(this);
linearLayout.addView(showtxt );
linearLayout.addView(txt1);
linearLayout.addView(txt2);
showtxt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String aaa= txt1 .getText().toString();//HOW TO ACCESS txt1 and txt2 from here
String bbb= txt2 .getText().toString();
}
}
}
while(somecondition)
I am almost new to android. How can I access to txt1 and txt2 in the click callback function?
You need to make the define the variables where they will have class wide scope:
public class Example extends Activity {
EditText txt1;
EditText txt2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
txt1 = new EditText(this);
txt2 = new EditText(this);
...
Now your onClick function will be able to see txt1 and txt2.
Alternatively
Since you appear to be creating a lot of txt1 and txt2 in one LinearLayout, you can pass your Button a reference to its EditTexts:
do {
...
// EditText[] array = { txt1, txt2 };
// is the short version of
EditText[] array = new EditText[2];
array[0] = txt1;
array[1] = txt2;
showtxt.setTag(array);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText[] array = (EditText[]) v.getTag();
String aaa = array[0].getText().toString();
String bbb = array[1].getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
} while(some condition)
This may not be not ideal, however without any further context I cannot guess your ultimate goal. Hope that helps!
Last Suggestion
If we call the Button and two EditTexts a row, you could store each row in a ViewGroup or View of its own. Say you wanted to have background colors for each row:
View row = new View(this); // or this could be another LinearLayout
row.setBackgroundColor(0x0000ff);
// Create and add the Button and EditTexts to row, as in row.addView(showtxt), etc
...
linearLayout.addView(row);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View row = v.getParent()
String aaa = ((EditText) row.getChildAt(1)).getText().toString();
String bbb = ((EditText) row.getChildAt(2)).getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
I'm trying to locally save an edittext text into a string variable I have created in another class within a PopupWindow like so:
I have an addEvent_Click method which displays a PopupWindow :
public void addEvent_Click(View view)
{
LayoutInflater inflater = (LayoutInflater) WhatsNewActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
eventPopup = new PopupWindow(inflater.inflate(R.layout.eventpopup, null, true), 480, 700, true);
eventPopup.showAtLocation(findViewById(R.id.whatsNew_Main), Gravity.CENTER_VERTICAL, 0, 0);
}
Now I want to save all my text fields locally to an event class via save button press in the PopupWindow like so:
public void btnSaveEvent_Click(View view)
{
final EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
final EditText timeEditText = (EditText) findViewById(R.id.timeEditText);
final EditText locEditText = (EditText) findViewById(R.id.locEditText);
final EditText sDateEditText = (EditText) findViewById(R.id.sDateExitText);
final EditText eDateEditText = (EditText) findViewById(R.id.eDateEditText);
final EditText catEditText = (EditText) findViewById(R.id.catEditText);
Event newEvent = new Event();
newEvent.eventName = nameEditText.getText().toString();
newEvent.time = timeEditText.getText().toString();
newEvent.location = locEditText.getText().toString();
newEvent.startDate = sDateEditText.getText().toString();
newEvent.endDate = eDateEditText.getText().toString();
newEvent.category = catEditText.getText().toString();
eventPopup.dismiss();
}
My problem is when I debug or run the code and it tries to execute the line:
final EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
my program crashes throwing an IllegalStateException error: Could not execute method of the activity
Any input would be appreciated, Thanks in advance.
Try changing nameEditText from final to private.
Try this:
private EditText nameEditText = (EditText) findViewById(R.id.nameEditText);