Im trying to get the String texts from the user and print it out in Toast - button

enter image description here
Hi I would some assistance on printing a user input to a toast message after a click of a button
`amountEditText = findViewById( R.id.amount_edit_text );
Editable userAmountInput = amountEditText.getText();
String amountInputString = userAmountInput.toString();
submit=findViewById( R.id.send_button );
submit.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText( MainActivity.this, ""+amountInputString, Toast.LENGTH_SHORT ).show();
}
} );`
I've tried to use Editable and String object retrieve the user input in a Toast message. But it prints nothing.

Related

JavaFX Alert not showing

I am trying to get an alert to pop up when my program receives the letter "A" over jserialcomm from my Arduino. I can confirm that my program receives the letter "A", as I have the program print a string to the console when it does, but the alert does not pop up. Has anyone seen this before?
#Override
public void serialEvent(SerialPortEvent serialPortEvent) {
if (serialPortEvent.getEventType() == SerialPort.LISTENING_EVENT_DATA_RECEIVED) {
byte[] temp = serialPortEvent.getReceivedData();
String message = new String(temp);
message = message.trim();
doneMessage = message;
System.out.println(message);
showAlert();
}
public void showAlert () {
System.out.println("Hello");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.showAndWait();
}
}

make a search in recylerview

I want to make a search on recyclerview, but the data appears if the phone is turned off and then turned on again, why is that?
the example photo
the app
When i'm search the recylerview is blank
but when the phones turned off and then turn on again, the data is appear
here's the code in fragment class
//search
editText = view.findViewById(R.id.searchServant);
searchButton = view.findViewById(R.id.button2);
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s = editText.getText().toString();
firebaseSearch(s);
}
});
//RecylerviewDatabaseServant
recyclerViewdatabase = view.findViewById(R.id.recyclerViewServant);
mManager = new LinearLayoutManager(getContext());
recyclerViewdatabase.setLayoutManager(mManager);
FirebaseRecyclerOptions<Servant> options =
new FirebaseRecyclerOptions.Builder<Servant>().setQuery(FirebaseDatabase.getInstance().getReference().child("Servant")
, Servant.class)
.build();
servantAdapter = new ServantAdapter(options);
recyclerViewdatabase.setAdapter(servantAdapter);
//End
here the function firebase search
private void firebaseSearch(String s) {
FirebaseRecyclerOptions<Servant> options = new FirebaseRecyclerOptions.Builder<Servant>().setQuery(FirebaseDatabase.getInstance()
.getReference().child("Servant").orderByChild("name").startAt(s).endAt(s + "\uf8ff"), Servant.class).build();
servantAdapter = new ServantAdapter(options);
recyclerViewdatabase.setAdapter(servantAdapter);
}
Try add this line in You search query
private void firebaseSearch(String s) {
FirebaseRecyclerOptions<Servant> options = new FirebaseRecyclerOptions.Builder<Servant>().setQuery(FirebaseDatabase.getInstance()
.getReference().child("Servant").orderByChild("name").startAt(s).endAt(s + "\uf8ff"), Servant.class).build();
servantAdapter = new ServantAdapter(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager({**context**}));
recyclerViewdatabase.setAdapter(servantAdapter);
servantAdapter.startListening();
}

Firebase Query not being updated

I want my firebase database link to be updated depending on what the user keys in inside the searchview but the link is not updated unless I open another activity and jump back to it.I have attacked my code in the bottom. So how do I refresh it automatically ?
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String query) {
query = sv.getQuery().toString();
Toast.makeText(MainMenu.this,query, Toast.LENGTH_SHORT).show();
makeItem();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
public void makeItem ()
{
lv = findViewById(R.id.listView);
db = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vsem-inventory.firebaseio.com/ItemList").orderByChild("ProductName").startAt(query).endAt(query+"\uf8ff");
FirebaseListOptions<ItemObject> options = new FirebaseListOptions.Builder<ItemObject>()
.setLayout(R.layout.content_main_menu_list)
.setQuery(db,ItemObject.class)
.build();
mAdapter = new FirebaseListAdapter<ItemObject>(options) {
#Override
protected void populateView(#NonNull View v, #NonNull ItemObject model, int position) {
final TextView tvAmount = v.findViewById(R.id.amount);
final TextView tvName = v.findViewById(R.id.name);
final TextView tvSerial = v.findViewById(R.id.serialNo);
final TextView tvSupplier = v.findViewById(R.id.supplierName);
final ImageView more = v.findViewById(R.id.more);
ImageView statusimg = v.findViewById(R.id.status);
Drawable paidIcon = v.getResources().getDrawable(R.drawable.succes);
Drawable lateIcon = v.getResources().getDrawable(R.drawable.late);
tvName.setText(model.getProductName());
tvSerial.setText(model.getSerialNo());
tvAmount.setText(model.getQuantity());
tvSupplier.setText(model.getModel());
final String Remarks = model.getRemarks();
final String cat = model.getCategory();
if(model.getQuantity().equals("0"))
statusimg.setImageDrawable(lateIcon);
else
statusimg.setImageDrawable(paidIcon);
more.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String serialNo = tvSerial.getText().toString();
String itemName = tvName.getText().toString();
String quan = tvAmount.getText().toString();
String supplier = tvSupplier.getText().toString();
showMenu(itemName,more,serialNo,quan,supplier,cat,Remarks);
}
});
}
};
lv.setAdapter(mAdapter);
}
The standard way is to call notifyDataSetChanged() after setting your adapter to your list view
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
mAdapter.notifyDataSetChanged();
Although I have seen some situations where only using this does not work and must be followed by these 2 commands.
lv.invalidateViews();
lv.scrollBy(0, 0);
And if all else comes to fail falling back on destroying and redrawing the list view might be your only viable option.
lv.destroyDrawingCache();
lv.setVisibility(ListView.INVISIBLE);
lv.setVisibility(ListView.VISIBLE);
EDIT : After looking at it a while more I just noticed you're missing listeners for your firebase. I assume you already have them somewhere as you already have the list but failing your refresh functions, what you can try is restarting the listeners whenever you're done with a query.
lv.setAdapter(mAdapter);
mAdapter.stopListening();
mAdapter.startListening();

Website loaded into JavaFX Webengine

I want my application to automatically log on the website kolotibablo,but the site does not exist tag form.What should I do to submit? Thanks.
#Override
public void initialize(URL url, ResourceBundle rb) {
//
WebEngine engine = browser.getEngine();
engine.load("https://www.kolotibablo.com/panel/login#login");
engine.setJavaScriptEnabled(true);
engine.documentProperty().addListener(new ChangeListener<Document>() {
#Override
public void changed(ObservableValue<? extends Document> observable, Document oldValue, Document newValue) {
engine.getDocument().getElementById("login").setAttribute("value", "User Name");
engine.getDocument().getElementById("password").setAttribute("value", "Password");
//Login
}
});
}
I succeeded in doing this by using the below code. I did not use your engine.documentPorperty() listener, so i removed that code altogether.
With the help of this answer I was able to do it with a bit of editing.
engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
if ( newState == Worker.State.SUCCEEDED ) {
Element login = engine.getDocument().getElementById("login");
if ( login != null ) {
login.setAttribute( "Value", "User_Name" );
System.out.println(login.getAttribute("Value"));
}
Element password = engine.getDocument().getElementById( "password" );
if ( password != null ) {
password.setAttribute( "Value", "Password" );
System.out.println(password.getAttribute("Value"));
}
HTMLElementImpl loginButton = (HTMLElementImpl) engine.getDocument().getElementById( "login_button" );
if ( loginButton != null ) {
loginButton.addEventListener("click", event -> {
System.out.println("here " + event.getType());
}, false);
loginButton.click();
}
}
});
So this will wait until the worker state is succeeded, and then grab the login element, set the attribute, grab the password element, set the attribute, grab the loginButton as an HTMLElementImpl, which allows us to simulate a click on it.
I have left in the print statements so that you can see things were being done as they happened.
Hope this helps.
Edit 1
So I revisited the problem and I came up with this solution also
String username = "User_Name";
String password = "Password";
String enterUserName = "document.getElementById('login').value='" + username + "';";
String enterPw = "document.getElementById('password').value='" + password + "';";;
engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
if ( newState == Worker.State.SUCCEEDED ) {
engine.executeScript(enterUserName);
engine.executeScript(enterPw);
}
});
The only thing I could NOT figure out is how to click on the login button. Everything I tried failed. So, I'm sorry about that. But, here is another way to do it also.
Please use
HTMLFormElement loginForm = ( HTMLFormElement ) webEngine.getDocument().getElementById("Login");
loginForm.submit();
To login , you have to submit the Form ,In above code "Login" is form id

Xamarin.Forms refresh TextProperty of Editor

Is there a way to change the text in an Editor cell after an event?
I have an Editor cell that shows an address from an SQLite database. I also have a button that gets the current address and shows this in an alert that asks if they would like to update the address to this. If Yes, then I would like to show the new address in the Editor cell.
public class UserInfo : INotifyPropertyChanged
{
public string address;
public string Address
{
get { return address; }
set
{
if (value.Equals(address, StringComparison.Ordinal))
{
return;
}
address = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
My code for the editor cell is
Editor userAddress = new Editor
{
BindingContext = uInfo, // have also tried uInfo.Address here
Text = uInfo.Address,
Keyboard = Keyboard.Text,
};
and then this after it has got the current address I have this
bool response = await DisplayAlert("Current Address", "Would you like to use this as your address?\n" + currAddress, "No", "Yes");
if (response)
{
//we will update the editor to show the current address
uInfo.Address = currAddress;
}
How do I get it to update the Editor cell to show the new address?
You are setting the BindingContext of the control, but not specifying a binding to go with it. You want to bind the TextProperty of the Editor to the Address property of your context.
Editor userAddress = new Editor
{
BindingContext = uinfo,
Keyboard = Keyboard.Text
};
// bind the TextProperty of the Editor to the Address property of your context
userAddress.SetBinding (Editor.TextProperty, "Address");
This may also work, but I'm not positive the syntax is correct:
Editor userAddress = new Editor
{
BindingContext = uinfo,
Text = new Binding("Address"),
Keyboard = Keyboard.Text
};

Resources