1

I'm trying it implement following Json string:

{ 
   "msg":[ 
      "football",
      "cricket",
      "baseball",
      "rugby",
      "gulf"
],
   "status":"success"
}

I have created the data classes as below:

class Sports(

    val msg : List<String>,
    val status : String
)

And

class Msg (

    val football : List<String>,
    val cricket : List<String>,
    val baseball : List<String>,
    val rugby : List<String>,
    val gulf : List<String>
)

Now I'm trying to get the objects and view it in a recyclerview list as per the tutorial.

How could I change it below & call it in the adapter?

interface PostApi {
    /**
     * Get the list of the pots from the API
     */
    @GET("/posts")
    fun getPosts(): Observable<List<Post>>
}

Edit:

MY adapter class as below:

    class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
        private lateinit var postList:Sports

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
            val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
            return ViewHolder(binding)
        }

        override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
            holder.bind(postList)
        }

        override fun getItemCount(): Int {
//Getting error in .isInitialied 'Unresolved reference'
            return if(::postList.isInitialized) postList.message.size else 0
        }

        fun updatePostList(postList: Sports){
            this.postList = postList
            notifyDataSetChanged()
        }

        class ViewHolder(private val binding: 
ItemPostBinding):RecyclerView.ViewHolder(binding.root){ //Getting error in root 'Unresolved reference'
            private val viewModel = PostViewModel()

            fun bind(post: Sports){
                viewModel.bind(post) //Getting error saying No value passed for parameter 'position'
                binding.viewModel = viewModel
            }
        }
    }
2
  • Your message model is useless. No need to create this Commented Oct 24, 2019 at 8:06
  • Using Gson, you have to do: val sports = Gson().fromJson(json, Sports::java.class); Commented Oct 24, 2019 at 8:09

1 Answer 1

1

If you get the Json from server then call it like below:

interface SportsApi {
    /**
     * Get the Sports from the API
     */
    @GET("/sports")
    fun getSports(): Observable<Sports>
}

Or if you want to to check it in locally then you have to convert this Json

Using Gson:

val sports = Gson().fromJson(json, Sports::java.class)

Using Moshi:

val sports = Moshi.Builder().build().adapter(Sports::java.class).fromJson(json)
Sign up to request clarification or add additional context in comments.

2 Comments

I've modified as you sugested and modified the adapter as well, but I'm getting few errors. Could you please help me to resolve them? I'm attached the adaptar in the Edit section of my question
Hi @Md. Asaduzzaman, I have resolve the errors but It's not outputting any results. Could you please let me know where it's going wrong? Asked as a separate question: stackoverflow.com/questions/58548925/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.