0

Receive SMS then set EditText to msgBody

public class SmsBroadcastReceiver extends BroadcastReceiver {
//.....
     ((EditText)MainActivity.mThis.findViewById(R.id.editTextName)).setText(msgBody);}

The error is this in View cannot be applied to android.view.View.Onclicklistiner

//onCreate
buttonSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            buttonSave.performClick(this);
        }
    });

the message will automatically save to SQLite and sync to Mysql when buttonSave is click

private void saveNameToServer() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Saving Name...");
    progressDialog.show();

    final String name = editTextName.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVE_NAME,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject obj = new JSONObject(response);
                        if (!obj.getBoolean("error")) {
                            //if there is a success
                            //storing the name to sqlite with status synced
                            saveNameToLocalStorage(name, NAME_SYNCED_WITH_SERVER);
                        } else {
                            //if there is some error
                            //saving the name to sqlite with status unsynced
                            saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    //on error storing the name to sqlite with status unsynced
                    saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("name", name);
            return params;
        }
    };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

@Override
public void onClick(View view) {
    saveNameToServer();
}

Are there other ways to auto click button when EditText value changes?

  • Invoking `buttonSave.performClick()` inside `buttonSave.onClickListener()` will just create an infinite loop. – PPartisan Feb 28 '18 at 16:17

1 Answers1

1

Instead of invoking the click buttonSave.performClick(this); just simply invoke saveNameToServer(); method to save your data.

buttonSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //buttonSave.performClick(this); // remove, not required
            saveNameToServer(); // save your data
        }
    });
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68