Question

I've only been working with Android for 2 weeks and I'm trying to create a simple homework assignment list to learn the basics. The code I have written displays information through an EditText and an ArrayList. When I go to one of my activities through my MainActivity and add information it shows up great. But when I go back to my MainActivity and back again to my previous activity. My data is deleted (or not displayed, I'm not sure). I'm sure I'm messing up something basic. If anyone is willing to offer some help or pointers, I'd greatly appreciate it. Thank You!

Here is my MainActivity Code:

package com.example.homeworkassignments;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class MainActivity extends Activity {

// instantiation of buttons
private Button btnDatabase;

    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
    ArrayList<String> listItems = new ArrayList<String>();

    //DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
    ArrayAdapter<String> adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        databaseButton();

    } // end onCreate()


    public void databaseButton() {
        //create database button
        btnDatabase = (Button)findViewById(R.id.databaseBtn);
        btnDatabase.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //start Database class when identify button is clicked
                Intent i = new Intent(MainActivity.this, Database.class);
                startActivity(i);
            }
        });
    } // end databaseButton()

Here is my Database Activity Class Code:

package com.example.homeworkassignments;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class Database extends ListActivity {

private Button addData;
private Button removeData;
private Button back;

//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_database);
   adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
   setListAdapter(adapter);

   addButton();
   removeButton();
   backButton();

} // end OnCreate()


public void addButton() {
    // create add button
        addData = (Button)findViewById(R.id.databaseAddBtn);
        addData.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           TextView myEdit = (TextView)findViewById(R.id.editText1);
                   String myEditValue = myEdit.getText().toString();

                   listItems.add(myEditValue);
                   adapter.notifyDataSetChanged();
                   myEdit.setText("");
        }

        });

  } // end addButton()


public void removeButton() {
    // create remove Button
    removeData = (Button)findViewById(R.id.databaseRemoveBtn);
    removeData.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (listItems.isEmpty()) {
                listItems.clear();
            }
            else {
                listItems.remove(0);
            }
            adapter.notifyDataSetChanged();
        }
    });
}


public void backButton() {
    // create back Button
    back = (Button)findViewById(R.id.databaseBackBtn);
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(Database.this, MainActivity.class);
            startActivity(i);   
        }
    });

    } // end backButton()


} // end Database class

And finally, here is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/databaseAddBtn"
        style="?@android:style/ButtonBar"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:text="@string/add_button" />

    <Button
        android:id="@+id/databaseRemoveBtn"
        style="?@android:style/ButtonBar"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:text="@string/remove_button" />

    <Button
        android:id="@+id/databaseBackBtn"
        style="?@android:style/ButtonBar"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="@string/back" />

</LinearLayout>

<EditText android:id="@+id/editText1"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:ems="10" />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false" />



</LinearLayout>

I understand it's a lot and probably very poorly written, but any help or advice would be extremely appreciated (I'm just trying to learn Android!)

Was it helpful?

Solution

ok i think u want to keep the arraylist constant in Database.java...

there are 2 cases of storage i can guess :-

  1. if u only want to store until you are executing the app and you allow the data be lost if the app is exited then in that case simply do this

    a. Declare an ArrayList like this public ArrayList<String> listItems = new ArrayList<String>(); in MainActivity.java and remove the same from Database.java.

    b. to add to this list you will access using MainActivity.listItems.add(object);

  2. You can try using public static ArrayList<String> listItems = new ArrayList<String>(); instead in MainActivity note the "static" it can hold data even if u exited the app.For more on static variables you can refer any tutorial.

  3. it should store the data permanently so dat even if you reboot you will have the data. if your case is this then u need to use database.

Hope it helps ....

thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top