mardi 24 septembre 2019

How to change the textview sequentially once the data inserted in database android

My requirement is,

I have Trailer Number 1 and name of the movie. Now once I inserted the data in table, I need to change the text as Trailer Number 2, this goes till Trailer Number 5. Because only 5 datas to be inserted. Also I will check whether the Trailer Number 1 is already existing or not in table. If not existing, I insert in Table but if exist, I change the text as Trailer Number 2. This is the exact scenario it should work.

But in my case, when I inserted Trailer Number 1, it jumps to Trailer Number 5 instead of not displaying Trailer Number 2.

After button click

boolean isFirstTrailerNumberExist = myDb.isExist("Trailer Number 1");
            boolean isSecondTrailerNumberExist = myDb.isExist("Trailer Number 2");
            boolean isThirdTrailerNumberExist = myDb.isExist("Trailer Number 3");
            boolean isFourthTrailerNumberExist = myDb.isExist("Trailer Number 4");
            boolean isFifthTrailerNumberExist = myDb.isExist("Trailer Number 5");

 if(isFirstTrailerNumberExist) {
                Toast.makeText(this, "Trailer Number 1 already Exist", Toast.LENGTH_SHORT)
                        .show();

            } else {
                boolean isInserted = myDb.insertData(trailerNumberName.getText().toString(),
                        trailerNumberCount.getText().toString());
                if(isInserted){

                    Toast.makeText(TrailerNumberActivity.this,"Inserted successfully",Toast.LENGTH_SHORT)
                            .show();
                    trailerNumberName.setText("Trailer Number 2");
                }
                else{
                    Toast.makeText(TrailerNumberActivity.this,"Insertion failed",Toast.LENGTH_SHORT)
                            .show();
                }
            }

if(isSecondTrailerNumberExist){
                Toast.makeText(this,"Trailer Number 2 already Exist",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                boolean isInserted = myDb.insertData(trailerNumberName.getText().toString(),
                        trailerNumberCount.getText().toString());
                if(isInserted){

                    Toast.makeText(TrailerNumberActivity.this,"Inserted successfully",Toast.LENGTH_SHORT)
                            .show();
                    trailerNumberName.setText("Trailer Number 3");
                }
                else{
                    Toast.makeText(TrailerNumberActivity.this,"Insertion failed",Toast.LENGTH_SHORT)
                            .show();
                }
            }
//the same repeats for third, fourth and fifth trailer too to check in db

if(isThirdTrailerNumberExist){
                Toast.makeText(this,"Trailer Number 3 already Exist",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                boolean isInserted = myDb.insertData(trailerNumberName.getText().toString(),
                        trailerNumberCount.getText().toString());
                if(isInserted){

                    Toast.makeText(TrailerNumberActivity.this,"Inserted successfully",Toast.LENGTH_SHORT)
                            .show();
                    trailerNumberName.setText("Trailer Number 4");
                }
                else{
                    Toast.makeText(TrailerNumberActivity.this,"Insertion failed",Toast.LENGTH_SHORT)
                            .show();
                }
            }

            if(isFourthTrailerNumberExist){
                Toast.makeText(this,"Trailer Number 4 already Exist",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                boolean isInserted = myDb.insertData(trailerNumberName.getText().toString(),
                        trailerNumberCount.getText().toString());
                if(isInserted){

                    Toast.makeText(TrailerNumberActivity.this,"Inserted successfully",Toast.LENGTH_SHORT)
                            .show();
                    trailerNumberName.setText("Trailer Number 5");
                }
                else{
                    Toast.makeText(TrailerNumberActivity.this,"Insertion failed",Toast.LENGTH_SHORT)
                            .show();
                }
            }

            if(isFifthTrailerNumberExist){
                Toast.makeText(this,"Trailer Number 5 already Exist",Toast.LENGTH_SHORT)
                        .show();
            }
            else {
                boolean isInserted = myDb.insertData(trailerNumberName.getText().toString(),
                        trailerNumberCount.getText().toString());
                if(isInserted){

                    Toast.makeText(TrailerNumberActivity.this,"Inserted successfully",Toast.LENGTH_SHORT)
                            .show();
                    trailerNumberName.setText("Trailer Number 1");
                }
                else{
                    Toast.makeText(TrailerNumberActivity.this,"Insertion failed",Toast.LENGTH_SHORT)
                            .show();
                }
            }

This is how I repeated to display till Trailer 5 but problem is after inserting Trailer 1, it jumps to Trailer 5.

Aucun commentaire:

Enregistrer un commentaire