Duarte Andrade Duarte Andrade - 6 months ago 9
MySQL Question

Insert values into same regist/row using more than one activity

I will give a example of what i want to achieve:

Lets say i have a table called "5Fruits" with 6 camps:

ID - Fruit1 - Fruit2 - Fruit3 - Fruit4 - Fruit5

Using 3 different activities (or more) i want to insert on first activity the fruit1, then i have a button "next" that opens another activity where i insert fruit2 and fruit 3 and then again, a button "next" that opens the last activity where i insert the fruit4 and fruit5.

And save the five fruits on the same row, regist, whatever.

How can i achieve this? It must be simple but im not figuring it out.
Thanks.

Answer

You can transfer some data between activities with putExtra method. Like that.

Define a stringArray in your mainActivity. And put Fruit1 in.

       String[] myFruits= new String[6];
       myFruits[0] = "apple";

       //Start your second activity.
       Intent intent = new Intent(this, secondActivity.class); 
       intent.putExtra("myExtras", myFruits); 
       startActivity(intent);

For your other activities.

String[] myFruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bla_bla_bla);

              //gets oldFruits.
    Intent intent = getIntent();
    myFruits = intent.getStringArrayExtra("myExtras");
}
private void sendFruits()
{
    myFurits[i] = "banana";  // "i" is your fruit number;
    Intent intent = new Intent(this, otherActivities.class);
    intent.putExtra("myExtras", myFruits);    //sends new fruits
    startActivity(intent);
}

Your Last Activity

private SQLiteDatabase sqw;
String[] myFruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bla_bla_bla);

              //gets oldFruits.
    Intent intent = getIntent();
    myFruits = intent.getStringArrayExtra("myExtras");
}



//if your id column is not AUTOINCREMENT, you can add query in.

String query = "INSERT INTO YOURTABLE (Fruit1,Fruit2,Fruit3,Fruit4,Fruit5)
        VALUES("'+myFruits[0]+'","'+myFruits[1]+'","'+myFruits[2]+'","'+myFruits[3]+'","'+myFruits[4]+'");";
sqw = new YOURDB().getWritableDatabas();   //get your db openhelper instance 
sqw.execSQL(query);    //your fruits inserted

It's not good way. Because using multiple activities for a just process that decreases your application performance. You can use with Fragments or dynamically creation Buttons and EditTexts and your process in a one Activity.