1

I have an issue where I have a listview, go to another activity to attach uservalues which than implements those values to the listview. However, if I go back to that activity and attach a new set of values, when I return to my listview activity, the new values are implemented but the old values have been removed. Would an sqlite database solve this problem? Or does it have something to do with the actual list being recreate?

ListActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.personalproject.peter.timerapp.TestingForAlarmData.TestAlarm;

import java.util.ArrayList;
import java.util.List;


public class ListOfAlarms extends ActionBarActivity {
    private static final int RESULT = 1000;

    List<TestAlarm> alarms = new ArrayList<>();
    String title;
    int totalTime;

    ListView listOfAlarms;
    ArrayAdapter<TestAlarm> alarmArrayAdapter;

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



       final TextView emptyViewForList = (TextView) findViewById(R.id.emptyTextViewForList);

       listOfAlarms = (ListView) findViewById(R.id.listView);
       alarmArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, alarms);
       listOfAlarms.setAdapter(alarmArrayAdapter);

//        if(listOfAlarms.getCount() <= 0){
//            emptyViewForList.setText("No Alarms Currently Available");
//            listOfAlarms.setEmptyView(emptyViewForList);
//        }

        listOfAlarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               alarms.get(position);

                Intent clockDownActivity = new Intent(ListOfAlarms.this, CountDownAct.class);
               clockDownActivity.putExtra("Title", title);
               clockDownActivity.putExtra("totalTime", totalTime);
                startActivity(clockDownActivity);
           }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_list_of_alarms, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void goToFillOut(View view) {

            Intent goingToFillOut = new Intent(this, Test.class);
            startActivityForResult(goingToFillOut, RESULT);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == RESULT && resultCode == RESULT_OK){


               title = data.getStringExtra("title");
                totalTime = data.getIntExtra("totalTime", 0);

                alarms.add(new TestAlarm(title, totalTime));

               alarmArrayAdapter.notifyDataSetChanged();

        }
    }


}

SecondActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Test extends ActionBarActivity {
    private static final String LOGTAG = "Test.class";
    private static final long timeInterval = 1000;


    private Button complete;
    private EditText titleEditText;
    private EditText hourEditText;
    private EditText minuteEditText;
    private EditText secondEditText;
    public static   int hour;



    public static int minute;
    public static int second;
    public static String title;




    public int actualTimeFiniliazedInMilliSeconds;


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

        titleEditText = (EditText) findViewById(R.id.titleEditText);
        hourEditText = (EditText) findViewById(R.id.hourEditText);
        minuteEditText = (EditText) findViewById(R.id.minuteEditText);
        secondEditText = (EditText) findViewById(R.id.secondEditText);

        complete = (Button) findViewById(R.id.completeButton);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void saveTimer(View view) {

        if(titleEditText.getText().toString().isEmpty() || hourEditText.getText().toString().isEmpty()
                || minuteEditText.getText().toString().isEmpty() || secondEditText.getText().toString().isEmpty()) {

            Toast.makeText(this, "Oops you forgot one", Toast.LENGTH_LONG).show();
            return;

        }
//            complete.setVisibility(View.GONE);
            title = titleEditText.getText().toString();
            hour = Integer.parseInt(hourEditText.getText().toString().trim());
            minute = Integer.parseInt(minuteEditText.getText().toString().trim());
            second = Integer.parseInt(secondEditText.getText().toString().trim());

            hour *= 3600000;
            minute *= 60000;
            second *= 1000;

        actualTimeFiniliazedInMilliSeconds = hour + minute + second;


        Intent intent = new Intent(Test.this, ListOfAlarms.class);
        intent.putExtra("title", title);
        intent.putExtra("totalTime", actualTimeFiniliazedInMilliSeconds);
        setResult(RESULT_OK, intent);
        finish();

    }



}

Alarm.java

public class TestAlarm {

    public String title;
    public int totalTime;

    public TestAlarm (String title, int totalTime) {
        this.title = title;
        this.totalTime = totalTime;
    }

    @Override
    public String toString() {
        return title;
    }
}
leonardo
  • 93
  • 2
  • 10

3 Answers3

0

What is happening is that when you leave and come back to the activity, Android is recreating the activity and listview. So you need some way to save the data over this change.

Using a sqlite database would solve the problem, since the databse would be permanent storage, and would save the information long term (for example, if the user shuts off the phone).

Another solution would be to implement onSaveInstanceState and onRestoreInstanceState, which will save information over changing activities (or orientation). If they shut off the phone the information would be lost.

From your question it doesn't sound like you need to keep information really long term, and implementing onSaveInstanceState is less work than sqlite.

Eugene Styer
  • 469
  • 3
  • 11
  • thanks for information, now where would I implement the onSaveInstanceStae and onRestoreInstanceState the ListView Activity or the userInput activity. And how should I go about adding new items to the listview? – leonardo Jul 15 '15 at 23:36
  • onSaveInstanceState and onRestoreInstanceState would be added to your main UI activity. Android will call onSaveInstanceState when it leaves your UI activity (so you can save your alarm list here). When it recreates the activity, it calls onRestoreInstanceState you can take that information and put it back into your alarm state. At that point your alarm list should be the same as when the activity was shut down, and you can add new entries the same as you do now. – Eugene Styer Jul 16 '15 at 13:27
0

You are creating a new instance of the Activity with

Intent intent = new Intent(Test.this, ListOfAlarms.class);

So everything is recreated.

To avoid this, you need to use an empty constructor when creating the Intent that will be sent back to the starting activity.

    Intent intent = new Intent(); // this should be empty
    intent.putExtra("title", title);
    intent.putExtra("totalTime", actualTimeFiniliazedInMilliSeconds);
    setResult(RESULT_OK, intent);
    finish();

See Going back to previous activity with different Intent value

and

The documentation

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I just did that and the listview is still not updating – leonardo Jul 16 '15 at 00:53
  • So when you go back to the activity with the LV, you have the same problem still, the old data isn't there but the new data is? Also, when you first create the activity alarms appears to be empty. And see my edit that this is the change you made – codeMagic Jul 16 '15 at 01:02
0

Simply try the following steps,don't need to use SqliteDB in this situation.

Create a Singleton class Eg :

    public class Singleton {
      private static Singleton uniqInstance;

      private  List<TestAlarm> mAlarms = new ArrayList<>();

      private Singleton() {
      }

      public static synchronized Singleton getInstance() {
        if (uniqInstance == null) {
          uniqInstance = new Singleton();
        }
        return uniqInstance;
      }

    public void setAllarms(List<TestAlarm> alarms){
        this.mAlarms  = alarms;
   }

public List<TestAlarm> getAllarms(){
  return this.mAlarms;
}

    }

Save the allarms on the singleton class when navigating to another activity

 listOfAlarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Singleton .getInstance().setAllarms(alarms); // Save the Collection

               alarms.get(position);

                Intent clockDownActivity = new Intent(ListOfAlarms.this, CountDownAct.class);
               clockDownActivity.putExtra("Title", title);
               clockDownActivity.putExtra("totalTime", totalTime);
                startActivity(clockDownActivity);
           }
        });

on the onActivityResult retrieve the saved collection from singleton class

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == RESULT && resultCode == RESULT_OK){


               title = data.getStringExtra("title");
                totalTime = data.getIntExtra("totalTime", 0);


               Singleton.getInstance().getAllarms().add(new TestAlarm(title, totalTime));

               this.alarms  = Singleton.getInstance().getAllarms();
               alarmArrayAdapter.notifyDataSetChanged();

        }
    }
J.R
  • 2,113
  • 19
  • 21
  • Great exact instructions I really appreciate your help so far. But now when I go to the fillout activity and return to the listview activity the data isn't showing completely. – leonardo Jul 16 '15 at 13:13