+-+-+ +-+ +-+-+-+-+
|G|O| |4| |H|I|V|E|
+-+-+ +-+ +-+-+-+-+

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Reimplementing Android Quiz App (UPDATE 2: Update and Delete Data)

BY: @princessdharmy | CREATED: June 12, 2018, 3:44 p.m. | VOTES: 67 | PAYOUT: $72.11 | [ VOTE ]

Repository

https://github.com/pars11/AndroidQuizApp

History

New Features

Update Category
@Query("UPDATE categories_table SET category = :category WHERE id = :id")
    void updateCategory(long id, String category);
@Override
    public void updateCategory(long id, String category) {
        categoryDao.updateCategory(id, category);
    }

@Override
    public void getCategoryToUpdate(Category category) {
       mCategoriesView.showCategoryToUpdate(category);
    }
Delete Category
@Query("DELETE FROM questions_table WHERE category_id = :categoryId")
    void deleteCategoryFromQuestion(long categoryId);

    @Query("DELETE FROM categories_table WHERE id = :id")
    void deleteCategory(long id);

From the code snippet above, you could notice that deletion of category is performed by two methods.
- Why?
The app allows a user add questions per category. Each category has a unique PrimaryKey. Category and Question have different database schema but to query the questions per category, category id needs to be stored in the Question object. Thereby creating relationship between the two schema.

@Entity(tableName = "questions_table", foreignKeys =
@ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "category_id"))
public class Question implements Serializable {

    @PrimaryKey(autoGenerate = true)
    long id;

    @NonNull
    @ColumnInfo(name = "category_id")
    long categoryId;

//other codes go in here

}
@Override
    public void deleteCategory(long categoryId) {
        categoryDao.deleteCategoryFromQuestion(categoryId);
        categoryDao.deleteCategory(categoryId);
    }

This will successfully delete the category from the database and the corresponding questions.

Update Question
@Query("SELECT * FROM questions_table WHERE id = :questionId")
    Question queryQuestion(long questionId);

    @Update
    void updateQuestion(Question question);

From the above code, the QuestionPresenter.java passes an intent to the AddQuestionFragment.java class to show the question for update. This class has access to the QuestionDao.java database to perform the update query. The below code shows the logic done by the presenter:

@Override
    public void fetchQuestionToUpdate(long id) {
        Question questionList = questionDao.queryQuestion(id);
        mView.showQuestionToUpdate(questionList);
    }

    @Override
    public void updateQuestion(Question question) {
        questionDao.updateQuestion(question);
    }

This updates the question and options successfully.

Delete Question
@Delete
    void deleteQuestion(Question question);
@Override
    public void deleteQuestion(Question question) {
        questionDao.deleteQuestion(question);
    }

This will successfully delete the question from the database and the corresponding options.

Short screen recording of the app

[IMAGE: https://cdn.steemitimages.com/DQmduppAQF5uZ6Vgkn36cdsPyvcJnfi2ot3JAuBskEgU9dx/videotogif_2018.06.12_15.09.02.gif]

Important Resources

Roadmap

Update

All thanks to @pars11 for bringing up such great idea that helps expand users' knowledge horizon. It's my pleasure to have contributed to and re-implemented the Android Quiz app. Thanks to Utopian.io for the opportunity to contribute to the Open Source ecosystem.

At this juncture, I would like to take this app to another phase that would make it more user friendly and flexible for a trivia quiz game.

TAGS: [ #utopian-io ] [ #development ] [ #steemit ] [ #steemdev ] [ #android ]

Replies

@codingdefined | June 13, 2018, 12:27 p.m. | Votes: 2 | [ VOTE ]

Thank you for your contribution. There are a lot of code which is commented out, it would be better if you just clean up your files.

And also where you are checking //These are sets of conditions for submit button visibility, here you have added three if else statement, I think you can actually club that in one if statement.

//These are sets of conditions for submit button visibility
if (dy > 0 && linearLayoutManager.findLastVisibleItemPosition() ==
   playQuizAdapter.getItemCount() - 1) {
   submitButton.setVisibility(View.VISIBLE);
} else if(dy == 0 && linearLayoutManager.findLastVisibleItemPosition() == 0 && (playQuizAdapter.getItemCount() - 1) == 0){
  submitButton.setVisibility(View.VISIBLE);
} else if(dy == 0 && linearLayoutManager.findLastVisibleItemPosition() == 1 && (playQuizAdapter.getItemCount() - 1) == 1){
   submitButton.setVisibility(View.VISIBLE);
} else {
 submitButton.setVisibility(View.GONE);
}

You can actually check if dy >= 0 amd then inside that you can write one more if statement, thus it will save some time of execution.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.

Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

@princessdharmy | June 13, 2018, 12:41 p.m. | Votes: 0 | [ VOTE ]

Thanks for the correction, would look into that.

@utopian-io | June 13, 2018, 2:07 p.m. | Votes: 0 | [ VOTE ]

Hey @codingdefined
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

@utopian-io | June 14, 2018, 3:10 a.m. | Votes: 0 | [ VOTE ]

Hey @princessdharmy
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>