Skip to content

Commit c886979

Browse files
committed
feat #11: show the list
1 parent 91e39f9 commit c886979

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

RecycleViewListApp/app/src/main/java/com/example/recycleviewlistapp/MainActivity.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,45 @@ package com.example.recycleviewlistapp
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.recyclerview.widget.RecyclerView
6+
import com.example.recycleviewlistapp.adapter.CardAdapter
57

68
class MainActivity : AppCompatActivity() {
9+
// creating objects
10+
private lateinit var recyclerView: RecyclerView
11+
private lateinit var cardAdapter: CardAdapter
12+
13+
// creating the list
14+
private var countryList = mutableListOf<String>()
15+
private var displayList = mutableListOf<String>()
16+
17+
718
override fun onCreate(savedInstanceState: Bundle?) {
819
super.onCreate(savedInstanceState)
920
setContentView(R.layout.activity_main)
21+
22+
// creating a list
23+
countryList.add("Bangladesh")
24+
countryList.add("Brazil")
25+
countryList.add("Bhutan")
26+
countryList.add("Nepal")
27+
countryList.add("India")
28+
countryList.add("United State")
29+
countryList.add("United Kingdom")
30+
countryList.add("United Arab Emirath")
31+
countryList.add("Norway")
32+
countryList.add("Japan")
33+
countryList.add("China")
34+
35+
// display the list
36+
37+
displayList.addAll(countryList)
38+
39+
// show the list into recycler view
40+
recyclerView = findViewById(R.id.recyclerView)
41+
// show the card adapter
42+
cardAdapter = CardAdapter(displayList)
43+
44+
recyclerView.adapter = cardAdapter
1045
}
1146
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.recycleviewlistapp.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.ImageView
7+
import android.widget.TextView
8+
import android.widget.Toast
9+
import androidx.recyclerview.widget.RecyclerView
10+
import com.example.recycleviewlistapp.R
11+
12+
/*
13+
1.our class will accept one parameter : country list => Mutable list
14+
2.then extend the class into recyclerview adapter which create view holder for CardAdapter class
15+
3.Create a inner class for accept the view into the Recycler view
16+
4.import the member methods
17+
18+
*/
19+
class CardAdapter(countryList: MutableList<String>) : RecyclerView.Adapter<CardAdapter.ViewHolder>() {
20+
// create a variable for the list
21+
private var countries: MutableList<String> = countryList
22+
23+
24+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
25+
val v = LayoutInflater.from(parent.context).inflate(R.layout.card_layout, parent, false)
26+
return ViewHolder(v)
27+
}
28+
29+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
30+
// access the item object
31+
holder.itemTitle.text = countries[position]
32+
}
33+
34+
override fun getItemCount(): Int {
35+
return countries.size
36+
}
37+
38+
// accept item in the view
39+
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
40+
var itemImage: ImageView = itemView.findViewById(R.id.itemImage)
41+
var itemTitle: TextView = itemView.findViewById(R.id.itemTitle)
42+
43+
init {
44+
// when clicked show the toast message
45+
itemView.setOnClickListener {
46+
val position: Int = adapterPosition
47+
Toast.makeText(itemView.context, "Clicked ${countries[position]}", Toast.LENGTH_LONG).show()
48+
49+
}
50+
}
51+
52+
// add click listener for the card view
53+
54+
55+
}
56+
}

RecycleViewListApp/app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
android:id="@+id/recyclerView"
1212
android:layout_width="match_parent"
1313
android:layout_height="match_parent"
14+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
1415
tools:layout_editor_absoluteX="201dp"
1516
tools:layout_editor_absoluteY="290dp" />
1617
</androidx.constraintlayout.widget.ConstraintLayout>

RecycleViewListApp/app/src/main/res/layout/card_layout.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:id="@+id/card_view"
66
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
7+
android:layout_height="wrap_content"
88
android:layout_margin="5dp"
99
app:cardBackgroundColor="#FF7043"
1010
app:cardCornerRadius="5dp"

0 commit comments

Comments
 (0)