离开活动后,数据会保存吗

时间:2015-07-15 02:43:54

标签: android sqlite listview android-listview

我有一个应用程序使用另一个ListView的用户输入数据填充Activity;但是,每当我将数据输入第二个Activity然后返回ListView活动时,我会看到它填充了我输入的值。但是,如果我再次返回第二个活动,输入值,然后返回ListView活动,我看到新值已更新,但旧值已消失。 现在,我不知道我是否正确更新listview,或者我是否需要使用sqlite数据库来存储此数据或共享首选项,以便在包含先前信息时将listview更新为新数据。

感谢您的帮助。

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;
}

}

1 个答案:

答案 0 :(得分:0)

好像你要替换listview中的新列表而不是更新列表视图的旧列表。

当您第一次来到活动A时,您应该创建一个全局数组列表,您可以在其中添加用户添加的所有项目。 现在,当用户继续使用活动B并添加任何项目时,您应该将值返回到活动A并在列表中添加该项目。

在列表中添加后,您应该将数据通知给适配器。所以,这里你的列表将有2个项目,它将显示在活动A 2个项目上。