This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carnot.recyclerviewlistadapter | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
lateinit var adapter: PostAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostAdapter : | |
ListAdapter<Post, PostAdapter.ViewHolder>(TaskDiffCallBack()) { | |
//This check runs on background thread | |
class TaskDiffCallBack : DiffUtil.ItemCallback<Post>() { | |
override fun areItemsTheSame(oldItem: Post, newItem: Post): Boolean { | |
Log.d(TAG,Thread.currentThread().name) | |
return oldItem.id == newItem.id; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User { | |
private String title; | |
private int id, userId; | |
} | |
//Mention the model class to which the data should be parsed if it is list or single just change | |
// the getType and same can be done for normal api requests | |
Rx2AndroidNetworking.get(URLEx).addPathParameter("user_id", "1").build(). | |
getObjectListSingle(User.class). | |
doOnSuccess(jsonArray -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final String URLEx = "https://jsonplaceholder.typicode.com/users/{user_id}/albums"; | |
Rx2AndroidNetworking.get(URLEx).addPathParameter("user_id", "1").build().getJSONArraySingle().doOnSuccess(jsonArray -> { | |
//Handle once data is available | |
}).doOnError(throwable -> { | |
//Handle in case of Error | |
}).subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final String URLEX = "https://jsonplaceholder.typicode.com/users/{user_id}/albums"; | |
AndroidNetworking.get(URLEx).addPathParameter("user_id","1").build().getAsJSONArray(new JSONArrayRequestListener() { | |
@Override | |
public void onResponse(JSONArray response) { | |
// Once data is received it will available for processing | |
} | |
@Override | |
public void onError(ANError anError) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseApp extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
AndroidNetworking.initialize(getApplicationContext()); | |
if (BuildConfig.DEBUG) { | |
//For logging api request and response | |
AndroidNetworking.enableLogging(HttpLoggingInterceptor.Level.BODY); |
NewerOlder