in my Android application,
I have 4 EditText where the user puts his information
And I want to create an other EditText, where all information in the 4 EditText are concatenated... But I really don't arrive to do that ....
Here is my code :
private Editable mtext1 = null;
private Editable mtext2 = null;
private Editable mtext3 = null;
private Editable mtext4 = null;
EditText mtext5;
PendingIntent mNfcPendingIntent;
IntentFilter[] mWriteTagFilters;
IntentFilter[] mNdefExchangeFilters;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
setContentView(R.layout.main);
findViewById(R.id.write_tag).setOnClickListener(mTagWriter);
//final Button quitBut = (Button) findViewById(R.id.button1);
mNoteBarCode = ((EditText) findViewById(R.id.noteBareCode));
mNoteAirport = ((EditText) findViewById(R.id.noteAirport));
mNoteFlightNumber = ((EditText) findViewById(R.id.noteFlightNumber));
mNoteName = ((EditText) findViewById(R.id.noteName));
mNote = ((EditText) findViewById(R.id.noteFull));
mtext1 = mNoteBarCode.getText();
mtext2 = mNoteAirport.getText();
mtext3 = mNoteFlightNumber.getText();
mtext4 = mNoteName.getText();
mtext5 = (EditText) mtext1 + (EditText) mtext2 + .......; // doesn't work
mtext5 = (EditText) mNote.getText();
String firstString = firstET.getText().toString(); // get string from first EditText
String secondString = secondET.getText().toString(); // get string from second EditText
EditText concatenatedET = (EditText) findViewById(R.id.your_edittext); //declare EditText you want them put in together
concatentatedET.setText(firstString + secondString); // put the string values in
Let me know if this works for you or if you need a better explanation
Related
i want when click on User info list view item (fill with adapter) , get user_id,
i use this code in main activity :
// long click on listview items
LIST_USER.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Clicked item id", " "+ id);
// Intent intent = new Intent(getBaseContext(), UpdateActivity.class);
// intent.putExtra("EXTRA_ID", id);
// startActivity(intent);
return true;
}
});
yet no problem !
but when send user_id to update activity database has stopped !!
use this code in update activity :
public class UpdateActivity extends AppCompatActivity {
private DatabaseHelper DB_HELPER;
private TextView TXT_VIEW;
private EditText EDT_NAME;
private EditText EDT_AGE;
private EditText EDT_GENDER;
private EditText EDT_PASS;
private EditText EDT_DESC;
private EditText EDT_PIC;
private Button BTN_UPDATE;
private Button BTN_DELETE;
private String STR_ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
TXT_VIEW = (TextView) findViewById(R.id.textView);
EDT_NAME = (EditText) findViewById(R.id.editText);
EDT_AGE = (EditText) findViewById(R.id.editText2);
EDT_GENDER = (EditText) findViewById(R.id.editText3);
EDT_PASS = (EditText) findViewById(R.id.editText4);
EDT_DESC = (EditText) findViewById(R.id.editText5);
EDT_PIC = (EditText) findViewById(R.id.editText6);
BTN_UPDATE = (Button) findViewById(R.id.button) ;
BTN_DELETE = (Button) findViewById(R.id.button2);
DB_HELPER = new DatabaseHelper(this);
STR_ID = getIntent().getStringExtra("EXTRA_ID");
TXT_VIEW.setText(STR_ID);
display user_id in log info :
thank's
The starting problem is in the types, you are passing a long and reading it as a string, the cast exception is raised in the onCreate and resulting in the crash. This should make it work:
private long STR_ID;
//...
STR_ID = getIntent().getLongExtra("EXTRA_ID");
TXT_VIEW.setText(String.valueOf(STR_ID));
I am beginner in android app development. My code is as follows- How to solve Number Format Exception in this code-
public class InterestActivity extends AppCompatActivity {
private EditText editAmount;
private EditText editInterest;
private EditText editDuration;
private TextView resultView;
private Button Calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interest);
editAmount = (EditText) findViewById(R.id.editAmount);
editInterest = (EditText) findViewById(R.id.editInterest);
editDuration = (EditText) findViewById(R.id.editDuration);
resultView = (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
}
public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = Integer.parseInt(editAmount.getText().toString());
int i = Integer.parseInt(editInterest.getText().toString());
int d = Integer.parseInt(editDuration.getText().toString());
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}
public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = 0;
int i = 0;
int d = 0;
try{
a= Integer.parseInt(editAmount.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editAmount"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
i = Integer.parseInt(editInterest.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editInterest"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
d = Integer.parseInt(editDuration.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editDuration"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}
You are assigning the Views to temporary local variables instead of the attributes of the class.
Short solution:
Remove EditText before editAmount = ...
Likewise with all the other assignments inside onCreate.
If you want to continue Android programming you should understand why this will work. This might help you: What is the difference between field, variable, attribute, and property in Java POJOs?
I have an Array List that i am getting Values from the EditText widget and placing them inside an Array List
I would like to do a Calculation and output the total to a Text View
The Program is Running fine it just crashes when i hit Calculate..
ArrayList<String> score = new ArrayList<String>();
public class MyActivity extends Activity {
EditText T1, T2, T3, T4, T5, T6, T7, T8, T9;
TextView total;
int sum = 0;
Button ButtonClear, ButtonScore;
ArrayList<String> score = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
T1 = (EditText) findViewById(R.id.editText2);
T2 = (EditText) findViewById(R.id.editText3);
T3 = (EditText) findViewById(R.id.editText4);
T4 = (EditText) findViewById(R.id.editText5);
T5 = (EditText) findViewById(R.id.editText6);
T6 = (EditText) findViewById(R.id.editText7);
T7 = (EditText) findViewById(R.id.editText8);
T8 = (EditText) findViewById(R.id.editText9);
T9 = (EditText) findViewById(R.id.editText10);
total = (TextView) findViewById(R.id.textView28);
ButtonScore = (Button) findViewById(R.id.buttonScore);
ButtonClear = (Button) findViewById(R.id.buttonClear);
score.add(T1.getText().toString());
score.add(T2.getText().toString());
score.add(T3.getText().toString());
score.add(T4.getText().toString());
score.add(T5.getText().toString());
score.add(T6.getText().toString());
score.add(T7.getText().toString());
score.add(T8.getText().toString());
score.add(T9.getText().toString());
public void Calculate(View v)
{
for (int i = 0; i < score.size(); i++)
{
sum = Integer.valueOf(score.get(i));
}
total.setText(sum);
}
You are setting integer value to textview therefore it crash,So change
total.setText(sum);
to
total.setText(String.valueOf(sum));
And to calculate sum do like
sum += Integer.valueOf(score.get(i));
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.
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);