How to get JSON object elements from GraphQL API call? [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have API result like in this image. I want to get key and value from extraInformation and apply them into a recycler view. What is the best way to do it?
https://i.stack.imgur.com/RFmSX.png

You can create a Model class for your Json response.
You can tale help from https://www.jsonschema2pojo.org/ site to create POJO class (model class)
And then when you are parsing the response you Gson Library (google's library to work with Json data) to parse your Json response to Model class and then when you have the Data in Java/Kotlin object pass that information to RecylerView Adapter and then call notifyDatasetchange on adapter.
ModelClass - > Parse Json to Object -> Pass to Adapter -> call Notify on adapter

Related

Is there any downsides to have BaseFragment with an argument? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
👋 I use Jatpack navigation, this is my declaration of BaseFragment:
abstract class BaseFragment<BINDING : ViewDataBinding, VIEW_MODEL : BaseViewModel>(private val layoutId: Int) : Fragment() {...}
so I can have fragments like this:
class DocumentsFragment : BaseFragment<FragmentDocumentsBinding, DocumentsViewModel>(R.layout.fragment_documents) {
My question is, we know we should never have parameters in Fragments to restore states and all, but could Base fragments have them?
Not in this case because there is now a constructor for doing exactly that
https://developer.android.com/reference/androidx/fragment/app/Fragment#Fragment(int)
I would recommend you to analyze it and try to adapt to it, you dont need it in the base fragment.
The parametyzed types thougth seem bad because if you wish to use other type of viewmodel you shoot your self in the foot. By example, does you base view model is a AndroidViewModel? You might need that but cant because you lock your self out. There are other view model types.
It's okay FragmentManager only needs Fragment classes that are added to it to have zero-argument constructor since they're the one that will be reflected.
It's irrelevant if that constructor proceeds to invoke super constructor with arguments.
Even androidx.Fragment has an overload that takes layoutId to inflate instead of overriding onCreateView:
class MyFragment : Fragment(R.layout.my_fragment)

Kotlin - Best way to update Android Views using retrieved Firebase data [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to update my Android views using some data that I've got on a Firebase Realtime DB. Since I'm using Kotlin, I've encountered some problems doing this simple thing. I'm not an expert on Realtime DB, I've just discovered that the calls to the DB are made asynchronously, and this means I've got to use the data inside the onDataChange function.
I also discovered that Kotlin doesn't let me change the value of a View that I passed as a parameter to some function (like the one I created to update that View using the data retrieved from the DB). So I'm counting on you for this.
What's the best way to retrieve data from Firebase and use that data to update my Views? I would like to have some clean code, like different functions that do the right thing, without having a pile of code in the onCreate method of my MainActivity.
Maybe something along these lines:
fun fetchFeed(): LiveData<List<Feed>> {
val result: MutableLiveData<List<Feed>> = MutableLiveData()
firestore.collection("feeds")
.addSnapshotListener { collectionSnapshot, exception ->
if (exception != null) {
//...handle error
return#addSnapshotListener
}
if (collectionSnapshot == null || collectionSnapshot.isEmpty) {
//...no data or no collection
return#addSnapshotListener
}
val value: MutableList<Feed> = arrayListOf()
collectionSnapshot.documents.map {
value.add(it.toObject(Feed::class.java))
}
data.postValue(value)
}
return result
}
class FeedFragmentViewModel: ViewModel() {
//ApiService is a singleton or an DAO eg... where your fetchFeed is declared or have access to it
val result: LiveData<List<Feed>> = ApiService.fetchFeed()
}
class FeedFragment: Fragment() {
override onActivityCreated() {
//initialize view model -> in docs
viewModel.result.observe(this, Observer {
myListAdapter.updateList(it)
})
}
}
It is not a complete solution but along these lines I think you should be able to incorporate realtimedb in it. Long story short you want to observe live data which represents the data from realtime db. When ever those c hange your callback in observer gets called where you can update the UI accordingly

How do i mock REST back end in an android app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My android app communicate with back end service through REST API . I want to mock out this API to quickly develop the front end. I am using android volley as client side networking library..
I would store mock data as json in my assets folder and create my data from these json files:
fun getJsonFromAssets(context: Context, jsonPath: String): String? {
return try {
context.assets.open(jsonPath).bufferedReader().use{
it.readText()
}
} catch (e: IOException) {
null
}
}
and then get the object like (here for example for a list):
val list = gson.fromJson<List<MyObject>>(jsonString, object : TypeToken<List<MyObject>>() {}.type)

Decode JSON file with Kotlin vs Swift [duplicate]

This question already has answers here:
Kotlin Data Class from Json using GSON
(3 answers)
Closed 4 years ago.
JSON decoding was improved in Swift4
you simply just call JSONDecoder().decode
and give it the json object and it will be converted into object
here is an example of that way
https://roadfiresoftware.com/2018/02/how-to-parse-json-with-swift-4/
my question that is there a way in Kotlin similar to the new way in Swift4 ?
Yes, Android has had that for quite a few years now.
It's a library from Google themselves, called GSON:
https://github.com/google/gson
Example:
Gson gson = new GsonBuilder().create();
gson.fromJson("MY JSON STRING", MyClass.class);
There are many overloads for the 'fromJson' function. There is also a 'toJson' function, to turn an object into a JSON string.
This is not just for Kotlin, it also works in Java.

How to retrieve data from a model class with kotlin? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I retrieve the data of a model class from an activity class?
This is the class:
class Mass(
val number: Number,
val date: String,
val day: String,
val reading: Reading
){
}
To access data of Model class i.e. class Mass. You just have to initialise above class in Activity and then you can directly access the variables of Mass Class.
like this :-
val mass : Mass = Mass(12,"08/02/2018","Firday")
textView.text=mass.day

Categories

Resources