Created
July 28, 2022 12:47
-
-
Save anandrex5/843460d14bb2ee3080ba5d7d2960090b to your computer and use it in GitHub Desktop.
Query Api Fetch using Adapter (Kotlin + Multiple Data Classes )
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
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".view.Activity.TestActivity"> | |
<TextView | |
android:id="@+id/news" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="News" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
android:layout_marginStart="15dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
/> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_marginTop="10dp" | |
app:layout_constraintTop_toBottomOf="@+id/news" | |
app:layout_constraintBottom_toBottomOf="parent" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
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.example.exam3.application.network | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
object ApiClient { | |
val BASE_URL : String = " https://newsapi.org/" | |
var retrofit: Retrofit? = null | |
fun getApiData(): Retrofit? { | |
if (retrofit == null) { | |
val interceptor = HttpLoggingInterceptor() | |
interceptor.level = HttpLoggingInterceptor.Level.BODY | |
val builder = OkHttpClient.Builder() | |
.addInterceptor(interceptor) | |
retrofit = Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.client(builder.build()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
return retrofit | |
} | |
} |
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.example.exam3.application.network | |
import com.example.exam3.model.datamodel.Article | |
import com.example.exam3.model.datamodel.PojoItems | |
import retrofit2.Call | |
import retrofit2.http.GET | |
import retrofit2.http.Query | |
interface ApiInterface { | |
//pass the query in the method | |
@GET("v2/everything") | |
fun getData(@Query ("q") a:String, | |
@Query ("from") date: Int, | |
@Query ("sortBy") sort:String, | |
@Query ("apiKey") key:String | |
) : Call<PojoItems> | |
} |
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.example.exam3.model.datamodel | |
import android.os.Parcelable | |
import kotlinx.android.parcel.Parcelize | |
@Parcelize | |
data class Article( | |
val author: String, | |
val content: String, | |
val description: String, | |
val publishedAt: String, | |
val title: String, | |
val url: String, | |
val urlToImage: String | |
): Parcelable |
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
plugins { | |
id 'com.android.application' | |
id 'org.jetbrains.kotlin.android' | |
id 'kotlin-kapt' | |
id 'kotlin-android-extensions' | |
} | |
android { | |
compileSdk 32 | |
defaultConfig { | |
applicationId "com.example.exam3" | |
minSdk 21 | |
targetSdk 32 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
buildFeatures { | |
dataBinding true | |
} | |
} | |
dependencies { | |
implementation 'androidx.core:core-ktx:1.7.0' | |
implementation 'androidx.appcompat:appcompat:1.4.2' | |
implementation 'com.google.android.material:material:1.6.1' | |
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | |
testImplementation 'junit:junit:4.13.2' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | |
implementation("androidx.recyclerview:recyclerview:1.2.1") | |
implementation("androidx.cardview:cardview:1.0.0") | |
implementation 'com.google.code.gson:gson:2.8.7' | |
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' | |
implementation("com.squareup.okhttp3:logging-interceptor:4.10.0") | |
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" | |
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0" | |
implementation "android.arch.lifecycle:viewmodel:1.1.0" | |
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | |
implementation 'androidx.appcompat:appcompat:1.4.2' | |
implementation 'com.github.bumptech.glide:glide:4.13.2' | |
kapt 'com.github.bumptech.glide:compiler:4.13.2' | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#FFFFFF" | |
> | |
<androidx.cardview.widget.CardView | |
android:layout_width="match_parent" | |
android:layout_height="250dp" | |
android:layout_margin="10dp" | |
app:cardElevation="5dp" | |
android:background="#FFFFFF" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#FFFFFF" | |
> | |
<ImageView | |
android:id="@+id/item_image" | |
android:layout_width="match_parent" | |
android:layout_height="120dp" | |
android:scaleType="fitXY" | |
android:layout_marginTop="5dp" | |
android:layout_marginHorizontal="10dp" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/item_date" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Date" | |
android:layout_marginTop="10dp" | |
android:textColor="#181819" | |
android:textSize="12dp" | |
app:layout_constraintTop_toBottomOf="@+id/item_image" | |
app:layout_constraintStart_toStartOf="@id/item_image" | |
app:layout_constraintBottom_toTopOf="@+id/item_description" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/item_description" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="5dp" | |
android:text="Description" | |
android:textSize="15dp" | |
android:textColor="#000" | |
app:layout_constraintStart_toStartOf="@id/item_date" | |
app:layout_constraintTop_toBottomOf="@id/item_date" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="@id/item_image" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.cardview.widget.CardView> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
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.example.exam3.view.activity | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import android.util.Log | |
import androidx.databinding.DataBindingUtil | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.example.exam3.R | |
import com.example.exam3.view.adapter.TestAdapter | |
import com.example.exam3.view.viewmodel.TestViewModel | |
import com.example.exam3.view.viewmodel.TestViewModelFactory | |
import com.example.exam3.application.network.ApiClient | |
import com.example.exam3.application.network.ApiInterface | |
import com.example.exam3.databinding.ActivityMainBinding | |
import com.example.exam3.model.datamodel.Article | |
import com.example.exam3.model.datamodel.PojoItems | |
import com.example.exam3.repository.PostRepository | |
//class MainActivity : AppCompatActivity() { | |
// override fun onCreate(savedInstanceState: Bundle?) { | |
// super.onCreate(savedInstanceState) | |
// setContentView(R.layout.activity_main) | |
// } | |
//} | |
class MainActivity : AppCompatActivity() { | |
lateinit var binding: ActivityMainBinding | |
lateinit var viewModel: TestViewModel | |
var list: List<Article> = ArrayList() | |
val adapter = TestAdapter(list) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) | |
val apiInterface = ApiClient.getApiData()?.create(ApiInterface::class.java) | |
val repository = PostRepository(apiInterface!!) | |
viewModel = ViewModelProvider(this, TestViewModelFactory(repository)).get(TestViewModel::class.java) | |
binding.recyclerView.adapter = adapter | |
binding.recyclerView.layoutManager = LinearLayoutManager(this) | |
viewModel.postList.observe(this, Observer { | |
Log.d("TAG", "List: $it") | |
adapter.setData(it) | |
}) | |
viewModel.getAllData() | |
} | |
} |
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.example.exam3.model.datamodel | |
import com.google.gson.annotations.SerializedName | |
data class PojoItems( | |
@SerializedName("articles") | |
val articles: List<Article>, | |
val status: String, | |
val totalResults: Int | |
) |
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.example.exam3.repository | |
import com.example.exam3.application.network.ApiInterface | |
class PostRepository( private val apiInterface: ApiInterface) { | |
//set the method query | |
fun getPost() = apiInterface.getData("tesla", 2022 - 6 - 26,"publishedAt","e323a6f898124786b6cf743a4987d0a8") | |
} |
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.example.exam3.model.datamodel | |
data class Source( | |
val id: String, | |
val name: String | |
) |
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.example.exam3.view.adapter | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import androidx.databinding.DataBindingUtil | |
import androidx.recyclerview.widget.RecyclerView | |
import com.bumptech.glide.Glide | |
import com.example.exam3.R | |
import com.example.exam3.databinding.ItemBinding | |
import com.example.exam3.model.datamodel.Article | |
class TestAdapter (var list: List<Article>): RecyclerView.Adapter<TestAdapter.ViewHolder>() { | |
var dataBinding: ItemBinding ? = null | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), | |
R.layout.item,parent ,false) | |
return ViewHolder(dataBinding!!) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val item: Article = list[position] | |
holder.bindData(item) | |
} | |
override fun getItemCount(): Int { | |
return list.size | |
} | |
fun setData(data: List<Article>){ | |
this.list = data | |
notifyDataSetChanged() | |
} | |
class ViewHolder (var binding: ItemBinding) : RecyclerView.ViewHolder(binding.root){ | |
fun bindData(article: Article) { | |
Glide.with(itemView.context) | |
.load(article.urlToImage) | |
.into(binding.itemImage) | |
binding.itemDate.text = article.publishedAt | |
binding.itemDescription.text = article.description | |
// binding.postTitle.text = pojoItem.title | |
// binding.post.text = pojoItem.body | |
// binding.postId.text = pojoItem.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
package com.example.exam3.view.viewmodel | |
import android.util.Log | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.example.exam3.model.datamodel.Article | |
import com.example.exam3.model.datamodel.PojoItems | |
import com.example.exam3.repository.PostRepository | |
import retrofit2.Call | |
import retrofit2.Callback | |
import retrofit2.Response | |
class TestViewModel(val repository: PostRepository): ViewModel() { | |
val postList = MutableLiveData<List<Article>>() | |
fun getAllData(){ | |
val response = repository.getPost() | |
response.enqueue(object : Callback<PojoItems> { | |
override fun onResponse(call: Call<PojoItems>, | |
response: Response<PojoItems>,){ | |
val data = response.body() as PojoItems | |
postList.value= (data.articles) | |
} | |
override fun onFailure(call: Call<PojoItems>, t: Throwable) { | |
Log.e("Fail", "${t.message}") | |
} | |
}) | |
} | |
} |
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.example.exam3.view.viewmodel | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProvider | |
import com.example.exam3.repository.PostRepository | |
class TestViewModelFactory(private val repository: PostRepository): ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
return TestViewModel(repository) as T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Params of API through Postman -
key value
q tesla
from 2022-06-26
sortBy publishedAt
apiKey e323a6f898124786b6cf743a4987d0a8