Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - How can I make it so that when I click on a layout, its data is copied and sent to Firebase?

I have an activity with a RecyclerView that contains information. I fill out a form, submit it, then the data is entered into Firebase and the Activity displays the item with the data that I entered. I want to make it so that when I click on any such item, it is copied to a separate table and then I can display it in another Activity. That is, I want to make an Add to Favorites button.

I've tried doing it myself, but I'm stumped.

In the adapter I tried to call the event handler and pass the data from the element I clicked on. But I'm sure this is wrong. And I don't know how to add to Firebase now.

 @Override
 public void onBindViewHolder(@NonNull CardAdapter.MyViewHolder holder, int position) {
    CardData userData = this.cardData.get(position);
    holder.favButn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Favourites.class);
                intent.putExtra("id", userData.getId());
                intent.putExtra("lastname", userData.getUserLastName());
                intent.putExtra("fullName", userData.getUserName());
                intent.putExtra("phone", userData.getUserPhone());
                intent.putExtra("email", userData.getUserEmail());

                v.getContext().startActivity(intent);
            }
        });
    }

If I do that, and if it works, as far as I understand it, there will only be 1 item on the page where I will be redirected after clicking. But I want to be able to add at least as many items to favorites on one screen (and they wouldn't disappear from here), then go to the Favourites screen and see them there.

How do I do that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first option to solve this is to save Favourites on the Firebase database in relation to your user. Each time you open the Favorites class just retrieve this information and apply them to your layout. This will always trigger wifi or network on your phone which will have some impact on battery life and/or optimization of your application.

My suggestion is to on the "addToFavorites" button you save your data to the local database or SharedPreferences. This way you can get this data even if the user doesn't have an internet connection.

Database

For a database, you can use SQLite or Room DB for Android. It's easy to use and you can implement it fast. Also, you can do a lot with this later if you need it in your app.

SharedPreferences

Using SharedPreferences you can save your favorites as String under some KEY like "Favourites". Each time you add a new one you can retrieve information from SharedPreferences based on your KEY, add a new value, and save it. In your Favourites class, you can just retrieve your KEY and use data to fill your layout. Here you can use something like JSONArray to save all your Favourites and then convert it to string to save it to SharedPreferences and from String convert it back to JSONArray. This is an easy process since JSONArray has .toString() and new JSONArray(string) methods and constructors.

Things to keep in mind
Firebase
Advantage: Clearing cache won't affect as data is stored online
Disadvantage: No internet no favourite list

SharedPreferences
Advantage: Clearing cache will flush out whole data
Disadvantage: Available all time, very fast


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...