0

I'm very new to programming, just a bit of a weekend hobby. I'm wanting to create an app that is like an interactive calendar, basically it works like this..

The main Activity displays a calendar where you can tap on a date and a new Activity will be launched. On this new Activity you can select a category for your item and an ImageButton will be added to the calendar date upon selecting the category. Once the button has been placed you can then tap on it and set extra information (alarm,place,details etc)

I have gotten as far as to do all the calendar layout, buttons to different activities and dynamically adding the calendar date ImageButton, but I have hit a dead end on how to save the data (the ImageButton) added by the user; 'Clicking button1 in activityB(e.g sport date) creates a dynamic image button of a baseball in activityA(calendar) layout, so my question is how do I save that data permanently(can be deleted by user).

I have spent the last few days reading over Onpause, onSaveinstancestate,onConfiguration,SQL,fragments and database. But I can't make heads or tails on how to apply this, let alone to apply it to an activity object created from another activity, I'd greatly appreciate any insight.

My code for the activity with the button that creates the dynamic button:

 package org.iimed.www;




    import android.os.Bundle;


    import android.view.View;
    import android.view.View.OnClickListener;

    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;

    import org.iimed.www.Sundayopen;
    import android.widget.RelativeLayout;
    import android.widget.TableLayout.LayoutParams;




    import android.app.Activity;
    import android.content.Intent;

    import android.graphics.Color;
    import android.graphics.drawable.Drawable;


        public class Penicillins extends Activity implements OnClickListener {

            ImageButton addmed,ab;
            RelativeLayout ll;




                public void onCreate(Bundle SavedInstanceState){
                     super.onCreate(SavedInstanceState);  
                     setContentView(R.layout.penicillin);
                     addmed =( ImageButton)findViewById(R.id.addmed);

                addmed.setOnClickListener(this);




                }
                public void onClick(View v) {

                    switch (v.getId()){

                    case R.id.addmed:


                         setContentView(R.layout.sundayopen);

                        ll =(RelativeLayout) findViewById(R.id.sundayopen); 




    android.widget.LinearLayout.LayoutParams b = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    int i = 0;
                ab = new ImageButton(this);

                ab.setImageResource(R.drawable.adaba);  
                ab.setBackgroundColor(Color.TRANSPARENT);
                ab.setLayoutParams(b);
                ab.setId(i) ;
                ab.getId();
                ab.setTag(1);
            ll.addView(ab);

                }


                }



                    }




My code for the activity the button appears in:



package org.iimed.www;



import java.util.ArrayList;

import android.R.string;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;

import android.content.Intent;

import android.view.View;


import android.widget.ImageButton;
import android.widget.RelativeLayout;


import android.view.View.OnClickListener;
public class Sundayopen extends Activity implements OnClickListener {   


    public static final Integer [] aba = {R.id.aba};

ImageButton ab;










public void onCreate(Bundle SavedInstanceState) {
  super.onCreate(SavedInstanceState);
              setContentView(R.layout.sundayopen);
         ImageButton hb1 = (ImageButton) findViewById(R.id.homebutton1);
         RelativeLayout  ll =(RelativeLayout) findViewById(R.id.sundayopen);
         ImageButton sbc = (ImageButton) findViewById(R.id.satlidopen);
         ImageButton abb = (ImageButton) findViewById(R.id.abbutton);
         sbc.setOnClickListener(this);
         hb1.setOnClickListener(this);
         abb.setOnClickListener(this);
        }





         public void onClick(View v){
             switch (v.getId()) {
             case R.id.homebutton1:
                startActivity(new Intent(this, MainActivity.class));
                 break;
             case R.id.satlidopen:
                 MediaPlayer media = MediaPlayer.create(Sundayopen.this, R.raw.openlid);
                 media.start();
                startActivity(new Intent(this,Iimedja.class));
                break;
             case R.id.abbutton:

                    startActivity(new Intent(this, ImageTextListViewActivity.class));

             }
         }





}
momo
  • 3,404
  • 6
  • 37
  • 66
ToeKnee
  • 47
  • 1
  • 10

1 Answers1

1

I believe you are asking 2 different things here:

  1. How to pass data between Activities (and how to restore Activities)
  2. How to save data permanently in your application

For the first one there are several examples out there. Check this answer on here. It looks to me that the things you have been reading about restoring Activities are not what you want to do (at least not yet). When Android needs to free memory it can kill your Activities, a good application will save the current status of the Activity on onSaveInstanceState() (ie. it will save the data it wants to restore when the Activity is launched again in the Bundle). Check Android Developer website on this.

Regarding the second one, you will need to save the data in the device memory (SQLite, filesystem or SharedPreferences). So basically you should save the data you want to recover, which from your explanation should be a group of:

  • Date
  • Category
  • Alarm time
  • Place
  • Details

If I were you I would create a SQLite database with a single Table containing the Columns you want to save. Save the data when a new item is added to the calendar, and load all the items data when the application launches.

Community
  • 1
  • 1
momo
  • 3,404
  • 6
  • 37
  • 66
  • Will that save the dynamically created ImageButton in activity1 created from activity 2? yeah I was looking at that example before but I just can't see how I can apply it to get/set ImageButton there is no function for that can I somehow assign a string or int to a dynamic button? also using that method how do I control the parameters of the button? such as where it goes? – ToeKnee Nov 03 '13 at 07:03
  • 1
    There's no straightforward way to save components like buttons. Everything you want to keep will need to be saved as primitives (String, integers, ...). I think the DB approach makes more sense in your case, saving one of your buttons would require to save all the associated data, then when going back to activity 1, you would load all buttons from DB and create them. Once you load that data, you know what date is that button in and what icon to show, you will also know all the info (detail, etc) you want to show when clicked, so you just create the listener to show them. – momo Nov 03 '13 at 09:19
  • Thanks a lot buddy, I'm in a bit over my head at the moment, so confused. Are there any examples of using a database like that you could link to me?or beginner guides? or anything definitive on the method I'd need or doesn't it really work like that? – ToeKnee Nov 03 '13 at 10:20
  • 1
    This example will probably help you http://stackoverflow.com/a/12015869/801913 or just search Google for android sqlite tutorial – momo Nov 04 '13 at 03:40
  • Thanks momo, I will definitely use this for my "date,category,alarm time,place,details" but I still can't break through this saving the ImageButton thing. In your answer you are saying that onSave and onRestore is not what I want to use, but the example you gave is dealing with putting an int in a bundle and put/getting it, but I can't put a dynamic ImageButton like this `ab=new ImageB ab.setImageResource(R.drawable.adaba);,ab.setBackgroundColor(Color.TRANSPARENT);,ab.setLayoutParams(b);ab.setId(i);,ab.getId(); ,ll.addView(ab);'with all it's settings – ToeKnee Nov 04 '13 at 05:56
  • Continued: as an int to put in a bundle. I can't find any examples anywhere of a dynamic button created in a different activity saving as persistent, and all the SQL examples/tutes are just adding and restoring strings of text.. – ToeKnee Nov 04 '13 at 05:57