Hot questions for Using Android EditText in arrays

Question:

I have EditText and array. I want fill array by values from EditText field. All the characters are digits. How can I achieve this? I've try this:

...
int [][] a = new int [100][100];
n=1;//now 1, but this value entered in EditText
...
                    if (edit_1.getText().length() > 0) {
                        for (int ii = 0; ii < a.length; ii++) {
                            a[ii][n+1] = Integer.parseInt(String.valueOf(edit_1.getText().charAt(ii)));
                        }
                    }
...
a[1][n+2]=a[1][n+1];
...

What i am getting in the LogCat:

05-13 23:40:55.046: E/AndroidRuntime(27281): FATAL EXCEPTION: main
05-13 23:40:55.046: E/AndroidRuntime(27281): Process: aectann.dcsmp, PID: 27281
05-13 23:40:55.046: E/AndroidRuntime(27281): java.lang.IndexOutOfBoundsException: charAt: 4 >= length 4
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:121)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at aectann.dcsmp.Zhegalkin_polynomial.zheg(Zhegalkin_polynomial.java:230)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at aectann.dcsmp.Zhegalkin_polynomial$1.onClick(Zhegalkin_polynomial.java:122)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.view.View.performClick(View.java:4811)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.view.View$PerformClick.run(View.java:20136)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.os.Handler.handleCallback(Handler.java:815)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.os.Handler.dispatchMessage(Handler.java:104)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.os.Looper.loop(Looper.java:194)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at android.app.ActivityThread.main(ActivityThread.java:5549)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at java.lang.reflect.Method.invoke(Native Method)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at java.lang.reflect.Method.invoke(Method.java:372)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
05-13 23:40:55.046: E/AndroidRuntime(27281):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

121 - function call;

230 - a[ii][n+1] = Integer.parseInt(String.valueOf(edit_1.getText().charAt(ii)));


Answer:

maybe this could be a solution for that Exception:

if (edit_1.getText().length() > 0) {
    for (int ii = 0; ii < edit_1.getText().length(); ii++) {
            a[ii][n + 1] = Integer.parseInt(String.valueOf(edit_1.getText().charAt(ii)));
        }
}

and if you want to create only space needed by those chars, maybe you will need to use something like this (but I think this will need to change how are you accessing the data):

int [][] a = new int [100][];
n=1;//now 1, but this value entered in EditText
...     
        if (edit_1.getText().length() > 0) {
            a[n+1] = new int[edit_1.getText().length()];
            for (int ii = 0; ii < a.length; ii++) {
                a[n+1][ii] = Integer.parseInt(String.valueOf(edit_1.getText().charAt(ii)));
            }
        }

hope it helps

Question:

How do I avoid hard coding a number/string in my java code. Example below the String/ phoneNumber?.

getApplication().startActivity( );

                String phoneNumber = " 1234567890 ";
                String message = " ";
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNumber, null, message, null, null);

Answer:

Based on the title, I assume you're trying to pull the phone number value out from an EditText.

Java:

EditText editText = (EditText) findViewById(R.id.myEditText);
String phoneNumber = editText.getText().toString();

XML:

<EditText
    android:id="@+id/myEditText"/>

Question:

Thanks for taking the time of reading my post.

How would i be able to have the value of an email in an EditText (emailEditText) to be saved from Create.java into this array in the activity AlarmService?

Where it says String[] toArr =

Is there anyone that is able to provide a solution or give any suggestions? I have tried multiple times without success.

Any help would be greatly appreciated! :)

AlarmService.java

package com.example.pc.achieve;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import static android.graphics.Color.GREEN;

public class AlarmService extends IntentService {

SharedPreferences pref;

PowerManager powerManager;
PowerManager.WakeLock wakeLock;

public AlarmService() {
    super("");
}

@Override
protected void onHandleIntent(Intent intent) {
    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FCFCFCFC");

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

    addNotification();
    sendMAIL();

}

public void addNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.icon_transperent)
                    .setLights(GREEN, 700, 700)
                    .setContentTitle("Alert!")
                    .setContentText("This is a reminder");

    Intent notificationIntent = new Intent(getApplicationContext(), MainMenu.class);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,     notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    builder.setVibrate(new long[] { 0, 1000, 1000, 1000, 1000 });
    manager.notify(0, builder.build());
}

public void sendMAIL(){

    String getEmail = pref.getString("email", "");
    String[] toArr = {getEmail};
    m.setTo(toArr);
    m.setFrom("notification@gmail.com");
    m.setSubject("Alert!");
    m.setBody("This is a reminder.");

    try {
        if(m.send()) {
        } else {
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
public void onDestroy() {
    super.onDestroy();
}
}

Create.java

package com.example.pc.achieve;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;

public class Create extends Activity {

SharedPreferences pref;

private EditText subjectEditText;
private EditText descEditText;
EditText emailEditText;

private DBManager dbManager;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);

        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

        subjectEditText = (EditText) findViewById(R.id.subject_edittext);
        descEditText = (EditText) findViewById(R.id.description_edittext);
        emailEditText = (EditText) findViewById(R.id.email_edittext);
}

public void btnCreate(View view) {

    String email = emailEditText.getText().toString();
    pref.edit().putString("email", email).apply();

    dbManager = new DBManager(this);
    dbManager.open();

    final String name = subjectEditText.getText().toString();
    final String desc = descEditText.getText().toString();

    dbManager.insert(name, desc);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your deadline has now been created.")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}


public void btnSetAlarm(View view) {

    startActivity(new Intent(Create.this, Alarm.class));
}
}

Answer:

You can use SharedPreference to get string value in another activity: In your EditText activity:

Declare globally:

SharedPreferences pref;

Initiate in onCreate() of both activity:

pref = PreferenceManager.getDefaultSharedPreferences(this); //Here is my change

Initialize editTexts in onCreate():

subjectEditText = (EditText) findViewById(R.id.subject_edittext);
descEditText = (EditText) findViewById(R.id.description_edittext);
emailEditText = (EditText) findViewById(R.id.email_edittext);

In btnCreate() method:

String email = emailEditText.getText().toString();
pref.edit().putString("email", email).apply();

In your String[] activity:

String getEmail = pref.getString("email", "");
String[] toArr = {getEmail};

Hope this helps.