I'm a beginner in developing android
I create a news feed where you can comment on posts, to do this I use firebase as a database and I use FirebaseRecyclerAdapter and FirebaseRecyclerOptions
I want the user to click on the "Comment" button of the first RecyclerView
The second RecyclerView displays the comments according to the post clicked.
I have no idea how to do this, knowing that I tried by capturing the position of the post.
Thank you. Thank you.
The first thing you'll need to do is record what pot the user clicked on. If you're using the Realtime Database, I recommend doing that by recording the key of the item that they clicked on. For some hints on how to do that, see:
Retrieving the Firebase key from onItemClick in FirebaseListAdapter
How to get obj key from FirebaseListAdapter on Item Click. FirebaseUI
Next up, you need to pass that key to the second recycler view somehow.
If the two recycler views are in the same activity, you can use a member field to store the comment that was clicked.
If the two recycler views are in separate activities, you can store the key in Android's shared preferences. For more on this data transfer mechanism, see:
SharedPreferences in the Android reference documentation
How to use SharedPreferences in Android to store, fetch and edit values
Finally, you'll need to use the key to load the comments for that specific post. This depends on your exact data structure, but I recommend starting with the documentation on working with lists of data or FirebaseUI, similar to what you likely already have.
Related
According to a response made by Yigit Boyar from Google, Live Data is not the best use case for a chat app, because it may lose displaying some items if they come at the same time. He recommends using the new Google's Paging Library. I've been using ItemKeyedDataSource for my inbox(all the people that have message the user), and the inside chat(the messages themselves). The problems are the following:
1- From the chat, when the user scrolls downwards, the user retrieves old messages, which means that the insertion of those messages should be in position 0 of the adapter, not sequentially like the paging library does. How can I alternate the position of the inserted items to be sequentially for new messages, and in position 0 for old messages?
2- From the inbox(people that have message the user), again I'm using ItemKeyedDataSource here, the problem is that I want to maintain the multiple document listener from the repository (I'm using Firebase Firestore), so I can detect every time a new person talks to the user. The problem is that callback.onResult is called only once, and fails when Firebase sends another user. How can I maintain an update-able list?
I understand that this answer is probably too late, but maybe it can help someone in future.
Position of item in RecyclerView is determined by the position of corresponding data object (of type T) inside PagedList<T>. PagedList is designed to look alike good old List<T>, but can be thought of as an "endless" list of elements.
PagedList gets its elements by pages on demand through something called DataSource.Factory. A Factory is used because DataSource by itself can only grow in one direction. If you need to prepend elements in PagedList, or change or remove existing elements you must invalidate the DataSource and a new instance will be created through DataSource.Factory.
So, to insert your data elements where you want them you should implement your own DataSource and DataSource.Factory by subclassing these base classes.
Note: Room, data persistence library from AndroidX, provides facilities to automatically generate instances of these classes for your data. You can write SQL query like this:
SELECT * FROM messages WHERE threadId=:threadId ORDER BY timestamp DESC
then get DataSource.Factory from this, use the factory to create LivaData<PagedList<Message>> and finally use the paged list to display messages in a RecyclerView in a chat application. Then, when you insert, update or remove data inside DB these changes will automatically propagate to the UI. This can be very useful.
I recommend you to read a few related examples a do codelabs:
https://codelabs.developers.google.com/codelabs/android-paging/#0
https://github.com/googlesamples/android-architecture-components/tree/master/PagingSample
https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample
I have a list of comment objects inside of an object. If I want to add a comment without touching the other comments how would I do that? I have used transactions in the past for editing an integer efficiently but I don't know if I can do that with a list. The end goal is for multiple users to be a ble to add comments at the same time without cutting eachother off (overriding their edits).
Here is an example of my database:
Solved it! Super simple. Just had to add push() to the end of my DatabaseRefercence like so. It gives every comment a key.
commentRef = pollRef.child("comments").push();
commentRef.setValue(comment);
I'm learning Android, and I'm building an activity that will receive users that are using different devices, and I would like to show all these users nicknames together, in a Listview.
So, I was thinking about how could I do that, and had the idea to use a Handler to update my listview every second, and to display the list of users in real time, for all users that are connected to the activity.
I created a Database using Phpmyadmin, and I'm using Json to print all the informations from the database, so I can use Java to get all the data.
My questions are:
Is there any better way of doing what I'm trying to do?
Will my App crash?
Does the handlers have a huge effect on the users battery?
Thank you.
Yes there is another way you can do it. You can look for RecyclerView . And notifyDataSetChanged() method. Also you can use notifyItemRemoved()bla bla ..
In RecyclerView you have an adapter and you give list of items to that adapter. When your item list changed , if you notify it to your adapter your recyclerview should change.
Android Recyclerview : RecyclerView
Also you can follow guides like in this blog : Creating lists with recyclerview
I am new to Android App development, working on an android app which populate a list of numbers, in a listview dynamically, depending on the choice of the user, but, the moment user closes the App, the items in the listview are lost. How can I maintain the state of the listview?
Examples with code would be highly appreciated.
When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help.
I think that for your purpose there are 3 main methods, i'll explain them from the easier to the most difficult (in my opinion).
Text File
A way to do this is to create two methods in a class:
one has to create the text file in the storage if it isn't created before and read that, the other has to append a String to a StringBuilder and write it on the previous text file.
For this method you need the uses-permission of reading and writing to storage.
This link can help you: http://developer.android.com/training/basics/data-storage/files.html
JSON (also XML)
With JSON file you can create a list of objects with your data that you can serialize when you update the list and deserialize when you want to read it. For this purpose you have to study JavaScript syntax or, at least, JSON one.
SQLite Database
Android SDK incorporate a class named SQLiteOpenHelper that you can extend to create a database inside your app.
This link can help you: http://developer.android.com/training/basics/data-storage/databases.html
There are also references saving methods but i think that aren't right for your purpose, they work betters to save something like preferences or single data like last login informations.
I went through your comment. I would personally suggest using SQLiteOpenHelper
You might need to understand the use of SQLite, hence the tutorial
Simple Flow. On your Activity 1 where person Add Friends save it to DB
Then refresh the List from the DB. So when you move to Activity 2 and come back again to Activity 1 your List will refresh from DB. Hence no loss of data as you want.
EDIT
As user wanted to know how to use the ListView with DB.
Following are my suggestion
Sai Geetha
Youtube
I am developing an app where I can Add Accounts, View Accounts and View Balances and View Transactions.
It is like a bankaccount for kids.
I want to be able to add the account name, account starting balance.
After that I want the names of the account in a listview when I click on View Accounts and the when I click on the list items I want to do some transactions (deposit and withdraw).
Also on ViewBalances, I want the same list view to pop up and I want to see the balances.
Each transaction, with a description, balance and date must be stored in a different array.
How can I save everything to arraylists and make it visible in the other activities?
Please help! :)
You want to have a central location where your arraylist is available to every Activity.
So you can have a central Object with static access methods to get the ArrayList and any other info you want to store. If you want to store it persistently you should use a Database or similar.
For that have a look at
http://developer.android.com/training/basics/data-storage/index.html
You can make you custom object class and make that parcelable. see here