0

I have two activities. EnterPin and ConfirmPin. The arrays are pin and confirmpin. I need to compare the two. I wanted to use a file to compare the arrays. Enterpin, would write to the file and Confirmpin is supposed to read and compare its array to the file. Is this possible? I've been doing some research, but nothing seems to fit what I am trying to do.

public class EnterPin extends Activity {
public int[] pin = new int[4];
public Button[] buttons = new Button[10];
public int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_pin);
    //getActionBar().setDisplayHomeAsUpEnabled(true);

    buttons[1] = (Button)findViewById(R.id.button1);
    buttons[2] = (Button)findViewById(R.id.button2);
    buttons[3] = (Button)findViewById(R.id.button3);
    buttons[4] = (Button)findViewById(R.id.button4);
    buttons[5] = (Button)findViewById(R.id.button5);
    buttons[6] = (Button)findViewById(R.id.button6);
    buttons[7] = (Button)findViewById(R.id.button7);
    buttons[8] = (Button)findViewById(R.id.button8);
    buttons[9] = (Button)findViewById(R.id.button9);
    buttons[0] = (Button)findViewById(R.id.button0);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_enter_pin, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public void button1(View view)
{
    if(counter < 3)
    {
        pin[counter] = 1;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button2(View view)
{
    if(counter < 3)
    {
        pin[counter] = 2;
        counter++;
    }
    else
    {
        PinEntered();
    }
} 
public void button3(View view)
{
    if(counter < 3)
    {
        pin[counter] = 3;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button4(View view)
{
    if(counter < 3)
    {
        pin[counter] = 4;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button5(View view)
{
    if(counter < 3)
    {
        pin[counter] = 5;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button6(View view)
{
    if(counter < 3)
    {
        pin[counter] = 6;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button7(View view)
{
    if(counter < 3)
    {
        pin[counter] = 7;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button8(View view)
{
    if(counter < 3)
    {
        pin[counter] = 8;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button9(View view)
{
    if(counter < 3)
    {
        pin[counter] = 9;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button0(View view)
{
    if(counter < 3)
    {
        pin[counter] = 0;
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void PinEntered()
{
   if(counter == 3)
   {
    TextView EnterPin = (TextView) findViewById(R.id.EnterPin);
    EnterPin.setText("Pin Entered");
    startActivity(new Intent("com.example.textsmslock.ConfirmPin"));
   }
}
public int GetPin(int pin)
{
    return pin;
}
}

.

public class ConfirmPin extends EnterPin {
public int[] pinconfirm = new int[4];
public Button[] buttons = new Button[10];
public int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_pin);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
    buttons[1] = (Button)findViewById(R.id.button1);
    buttons[2] = (Button)findViewById(R.id.button2);
    buttons[3] = (Button)findViewById(R.id.button3);
    buttons[4] = (Button)findViewById(R.id.button4);
    buttons[5] = (Button)findViewById(R.id.button5);
    buttons[6] = (Button)findViewById(R.id.button6);
    buttons[7] = (Button)findViewById(R.id.button7);
    buttons[8] = (Button)findViewById(R.id.button8);
    buttons[9] = (Button)findViewById(R.id.button9);
    buttons[0] = (Button)findViewById(R.id.button0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_confirm_pin, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public void button1(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 1;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button2(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 2;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
} 
public void button3(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 3;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button4(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 4;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button5(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 5;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button6(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 6;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button7(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 7;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button8(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 8;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button9(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 9;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button0(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = 9;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}

public void ConfirmingPin()
{

    /*for(int i = 0; i < 4; i++)
    {
        if(pinconfirm[i] != pin[i])
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pins Do Not Match!!!!");


            startActivity(new Intent("com.example.textsmslock.ConfirmPin"));
        }*/
        if(pinconfirm.equals((pin))) == true)
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pin Confirmed");
            startActivity(new Intent("com.example.textsmslock.PinComfirmation"));
        }
        else
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pins Do Not Match!!!!");

            startActivity(new Intent("com.example.textsmslock.ConfirmPin"));

        }

}
}

this was how how I was trying to do it at first with just arrays

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • You should look at that http://stackoverflow.com/questions/4670215/how-to-serialize-arraylist-on-android – Jeremy D Nov 20 '12 at 21:48

2 Answers2

0

To me it sounds like SharedPreferences is what you are looking for. It is a lightweight feature for storing data.

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
0

Does it need to be an array?

If it's just a String, all you need is to pass your value with an intent:

//EnterPinActivity
Intent intent=new Intent(EnterPinActivity.this, ConfirmPinActivity.class);
intent.putExtra("pin", stringWithYourPinCode);
startActivity(intent);

//and then, in your ConfirmPinActivity
Intent intent=getIntent();
String yourPin=intent.getStringExtra("pin");

If it absolutely has to be an array, you can still pass it with your intent using the same putExtra() method.

My point is that passing data between two activities by attaching it to the Intent is way better practice (faster and more efficient) than using file or SharedPreferences (which also includes file I/O operations).

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
  • That is what I was thinking about doing, but once the android application closes, will the pin still be there? – DaJaynae Williams Nov 20 '12 at 22:06
  • Well, that's when you use SharedPreferences, but you will only need to store your PIN if it is confirmed (in ConfirmPinActivity). The best way to pass PIN between two activities is to use intent extras. – Andrii Chernenko Nov 20 '12 at 22:12
  • Since you are going to persist your PIN, you can use SharedPreferences in EnterPin activity, but then you will need to implement additional logic in your ConfirmPin activity to delete stored PIN in case it is not confirmed or user changes his mind. Which is perfectly legal, but seems messy and unclear to me. – Andrii Chernenko Nov 20 '12 at 22:16
  • 1
    Remember if you store the pin in a shared preferences object to set the security mode to private: http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int) failure to do this will mean that it can be accessed from outside your application. – Paul Harris Nov 20 '12 at 23:01