0

I am a beginner to this field of android and am currently in pursuit of developing an application to track the number of periods that i have bunked with the use of counters and the subject names in edittext and the values of counters keeps on incrementing as the button bunked is pressed everytime and its change will be effected in a textfield respectively my app has only one activity and when the user has finished entering the data in edittext and as the counters are incremented so do the values in textview and when the user presses the back button the app exits and now when the user enters the app once again now all the previously entered data are gone... no data in edittext or textview either...

public class MainActivity extends Activity {

    public static final String sub1="sub";
    public static final String count1="Count";
    String sub[]=new String[10];
    String count[]=new String[10];
    int counter[]=new int[10];
    Button button[]=new Button[10];
    EditText et[]=new EditText[10];
    TextView tv[]=new TextView[12];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        button[0] = (Button) findViewById(R.id.button1);
        button[1] = (Button) findViewById(R.id.button2);
        button[2] = (Button) findViewById(R.id.button3);
        button[3] = (Button) findViewById(R.id.button4);
        button[4] = (Button) findViewById(R.id.button5);
        button[5] = (Button) findViewById(R.id.button6);
        button[6] = (Button) findViewById(R.id.button7);
        button[7] = (Button) findViewById(R.id.button8);
        button[8] = (Button) findViewById(R.id.button9);

        tv[0] = (TextView) findViewById(R.id.textView2);
        tv[1] = (TextView) findViewById(R.id.textView3);
        tv[2] = (TextView) findViewById(R.id.textView4);
        tv[3] = (TextView) findViewById(R.id.textView5);
        tv[4] = (TextView) findViewById(R.id.textView6);
        tv[5] = (TextView) findViewById(R.id.textView7);
        tv[6] = (TextView) findViewById(R.id.textView8);
        tv[7] = (TextView) findViewById(R.id.textView9);
        tv[8] = (TextView) findViewById(R.id.textView10);
        tv[9] = (TextView) findViewById(R.id.textView1);

        et[0] = (EditText) findViewById(R.id.editText1);
        et[1] = (EditText) findViewById(R.id.editText2);
        et[2] = (EditText) findViewById(R.id.editText3);
        et[3] = (EditText) findViewById(R.id.editText4);
        et[4] = (EditText) findViewById(R.id.editText5);
        et[5] = (EditText) findViewById(R.id.editText6);
        et[6] = (EditText) findViewById(R.id.editText7);
        et[7] = (EditText) findViewById(R.id.editText8);
        et[8] = (EditText) findViewById(R.id.editText9);

        for(int l=0;l<9;l++) {
            sub[l]=et[l].getText().toString();
            et[l].setText(sub[l]);
        }

        for (int j = 0; j < 9; j++) {
            button[j].setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for(int k=0;k<9;k++) {
                    if(button[k]==arg0.findViewById(R.id.button1)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }           

                    if(button[k]==arg0.findViewById(R.id.button2)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }

                    if(button[k]==arg0.findViewById(R.id.button3)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button4)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button5)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button6)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button7)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button8)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                   

                    if(button[k]==arg0.findViewById(R.id.button9)) {
                        counter[k]++;
                        tv[k].setText(""+counter[k]);
                    }                               
               }
          }
    });
  }
}

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString(sub1,sub[0]);
        savedInstanceState.putInt(count1, counter[0]);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        sub[0]=savedInstanceState.getString(sub1);
        counter[0]=savedInstanceState.getInt(count1);
    }
}
3
  • use shared preferences or store the values in database. Jul 10, 2013 at 8:30
  • whoa, break phrases and use punctuation :-/
    – orique
    Jul 10, 2013 at 8:46
  • JSON is an alternative, or write data to file by yourself
    – yushulx
    Jul 10, 2013 at 9:00

1 Answer 1

0

Its happening because you are saving all data in Arrays which get cleaned up when the app exits. There are several ways tht you could do this. You could either use SharedPreferences to save your data or write your values to a file. For e.g. you could use SharedPreferences as follows:

public static final String MY_PREF = "MyPreference";
SharedPreferences.Editor editorPref;
public static String keyName = "MyKey";
SharedPreferences myPref;

You could then create the shared preference and add values to it.Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.