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);
Related
I followed a tutorial on how to update items in firebase, but when I try, it only adds a new item with the new edited name. It doesnt delete the one I was going to update.
Here is my code for the alertdialog (that opens when I long click an item in the listview):
private void updateDialog(final String verktøynavn, final String verktøynr, final String verktøytype, final String verktøystatusibil, final String utlånttil, final String verktøystatusutlant) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.endreslette_dialog, null);
dialogBuilder.setView(dialogView);
final TextView tvverktøynavn = (TextView) dialogView.findViewById(R.id.tvverktøynavn);
final EditText etverktøynavn = (EditText) dialogView.findViewById(R.id.etverktøynavn);
final EditText etverktøynr = (EditText) dialogView.findViewById(R.id.etverktøynr);
final Spinner spverktøytype = (Spinner) dialogView.findViewById(R.id.spverktøytype);
final ImageView ivoppdater = (ImageView) dialogView.findViewById(R.id.ivoppdater);
final ImageView ivslettverktøy = (ImageView) dialogView.findViewById(R.id.ivslettverktøy);
dialogBuilder.setTitle(verktøynavn);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
ivoppdater.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String navn = etverktøynavn.getText().toString().trim();
String nr = etverktøynr.getText().toString().trim();
String type = spverktøytype.getSelectedItem().toString();
if(TextUtils.isEmpty(verktøynavn)){
etverktøynavn.setError("Navn mangler");
return;
}
updateverktoy(navn, nr, type, verktøystatusibil, verktøystatusutlant, utlånttil);
}
});
}
private boolean updateverktoy(String id, String navn, String nr, String type, String statusibil, String statusutlant) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Verktøy").child(id);
Verktoy verktoy = new Verktoy(id, navn, nr, type, statusibil, statusutlant);
databaseReference.setValue(verktoy);
Toast.makeText(this, "Verktøy endret", Toast.LENGTH_LONG).show();
return true;
This is happening because you are using setValue() method instead of updateChildren() method. If the id is different every time when you call the updateverktoy(), then a new child will be added, if the id the is the same, that child will be overridden. So in order to update a child, you need to use the same id, a Map that contains the new values that you want to update and then call updateChildren() on that particular reference.
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));
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);
}
});
I am pretty new with Android and I am learning it by reading a Deitel book. I decided to develop an app with tabs and I am using Fragments (as you can see).
I read on my book/on the internet how to implement an onClick event for a button, but when I try to run the app in my Android phone, it crashes.
Code
public class GamesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_games, container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText val1 = (EditText) v.findViewById(R.id.editText1);
EditText val2 = (EditText) v.findViewById(R.id.editText2);
EditText val3 = (EditText) v.findViewById(R.id.editText3);
EditText sol1 = (EditText) v.findViewById(R.id.editText4);
EditText sol2 = (EditText) v.findViewById(R.id.editText5);
//Solve the equation using a method of a class I created
equazioni2grado Equazione = new equazioni2grado();
Equazione.solveEquation(val1.getText().toString(), val2.getText().toString(), val3.getText().toString());
//Show the results
sol1.setText( Equazione.getSol1AsFraction() );
sol2.setText( Equazione.getSol1AsFraction() );
}
});
return view;
}
}
XML
link
I put the XML in a pastebin because it's pretty long. The XML looks like this:
I read that I can define the onClick event in the XML or in the java file, both ways are possible. I am doing it programmatically as you can see.
I can't find where is my mistake. Any idea?
Note: The object Equazione has the method that is:
public void solveEquation(String x, String y, String z) { ... }
EditText val1 = (EditText) v.findViewById(R.id.editText1);
Use view instead of v as below
EditText val1 = (EditText) view.findViewById(R.id.editText1);
try the code below:
public class GamesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_games, container, false);
Button button = (Button) view.findViewById(R.id.button1);
EditText val1 = (EditText) view.findViewById(R.id.editText1);
EditText val2 = (EditText) view.findViewById(R.id.editText2);
EditText val3 = (EditText) view.findViewById(R.id.editText3);
EditText sol1 = (EditText) view.findViewById(R.id.editText4);
EditText sol2 = (EditText) view.findViewById(R.id.editText5);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Solve the equation using a method of a class I created
equazioni2grado Equazione = new equazioni2grado();
Equazione.solveEquation(val1.getText().toString(), val2.getText().toString(), val3.getText().toString());
//Show the results
sol1.setText( Equazione.getSol1AsFraction() );
sol2.setText( Equazione.getSol1AsFraction() );
}
});
return view;
}
}
And can you paste LogCat?
You need to call findViewById on the view that represents the layout you've inflated. In this case, it's the object "view", returned from inflater.inflate().
One way of doing that is declaring view as a final variable and then replacing the calls "v.findViewById..." with "view.findViewById...".
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