Skip to content

Commit c859401

Browse files
Bug fixed and delete boid added
1 parent a303f14 commit c859401

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ android {
1313
minSdk 26
1414
targetSdk 30
1515
versionCode 2
16-
versionName "2.0.0"
16+
versionName "2.0.1"
1717

1818
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1919
}

app/src/main/java/com/heycode/iporesult/activities/MainActivity.kt

+6-13
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ class MainActivity : AppCompatActivity(), BoidAdapter.OnItemClickListener {
7171
storedBoids = dataFromSharedPref(this@MainActivity)
7272
.getStringSet(USER_BOID_SET, null) as MutableSet<String>
7373

74-
75-
// storedBoids = arrayOf("1301630000134578","1301590000282887")
7674
binding.apply {
7775
if (storedBoids.isNotEmpty()) {
7876
rvSavedBoid.apply {
7977
val myAdapter =
8078
BoidAdapter(storedBoids.toTypedArray(), this@MainActivity)
8179
adapter = myAdapter
8280
layoutManager = LinearLayoutManager(this@MainActivity)
81+
setHasFixedSize(false)
8382
}
8483
} else {
8584
Toast.makeText(this@MainActivity, "Can not load data", Toast.LENGTH_SHORT)
@@ -157,7 +156,7 @@ class MainActivity : AppCompatActivity(), BoidAdapter.OnItemClickListener {
157156
}
158157

159158
override fun onItemClick(position: Int) {
160-
Toast.makeText(this, "hhhhh", Toast.LENGTH_SHORT).show()
159+
// Toast.makeText(this, "hhhhh", Toast.LENGTH_SHORT).show()
161160
binding.tietBoid.setText(storedBoids.toTypedArray()[position])
162161
}
163162

@@ -241,19 +240,13 @@ class MainActivity : AppCompatActivity(), BoidAdapter.OnItemClickListener {
241240
.setPositiveButton(
242241
"Yes"
243242
) { d, _ ->
244-
val new = ArrayList<String>()
245-
storedBoids.forEach {
246-
if (it.isNotBlank()) {
247-
if (storedBoids.elementAt(position) != it) {
248-
new.add(it)
249-
}
250-
}
251-
}
243+
storedBoids.remove(storedBoids.elementAt(position))
252244
editorFromSharedPref(this@MainActivity).putStringSet(
253245
USER_BOID_SET,
254-
new.toMutableSet()
246+
storedBoids
255247
)
256-
binding.rvSavedBoid.adapter?.notifyItemChanged(position)
248+
val ss= BoidAdapter(storedBoids.toTypedArray(), this@MainActivity)
249+
binding.rvSavedBoid.adapter = ss
257250
d.dismiss()
258251
}
259252
.setNegativeButton(

app/src/main/java/com/heycode/iporesult/adapters/BoidAdapter.kt

+27-27
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ import android.view.ViewGroup
66
import android.widget.TextView
77
import androidx.recyclerview.widget.RecyclerView
88
import com.heycode.iporesult.R
9+
import com.heycode.iporesult.databinding.BoidItemBinding
910
import de.hdodenhof.circleimageview.CircleImageView
1011

1112
class BoidAdapter(
1213
private val names: Array<String>,
1314
private val listener: OnItemClickListener,
1415
) : RecyclerView.Adapter<BoidAdapter.ViewHolder>() {
1516

17+
18+
interface OnItemClickListener {
19+
fun onItemClick(position: Int)
20+
fun onItemLongClick(position: Int)
21+
}
22+
1623
// create new views
1724
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
1825
// inflates the card_view_design view
1926
// that is used to hold list item
20-
val view = LayoutInflater.from(parent.context)
21-
.inflate(R.layout.boid_item, parent, false)
22-
23-
return ViewHolder(view)
27+
val binding = BoidItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
28+
return ViewHolder(binding)
2429
}
2530

2631
// binds the list items to a view
@@ -29,17 +34,15 @@ class BoidAdapter(
2934
val item1 = names[position]
3035

3136
// sets the image to the imageview from our itemHolder class
32-
holder.image.setImageResource(R.drawable.logo)
33-
// Glide
34-
// .with(context)
35-
// .load(LOGO_IMG)
36-
// .fitCenter()
37+
holder.apply {
38+
image.setImageResource(R.drawable.logo)
39+
title.text = item1
40+
subtitle.text = item1
41+
}
42+
// Glide.with(context).load(LOGO_IMG).fitCenter()
3743
// .placeholder(R.drawable.logo)
3844
// .into(holder.image)
3945

40-
// sets the text to the textview from our itemHolder class
41-
holder.title.text = item1
42-
holder.subtitle.text = item1
4346
}
4447

4548
// return the number of the items in the list
@@ -48,30 +51,27 @@ class BoidAdapter(
4851
}
4952

5053
// Holds the views for adding it to image and text
51-
inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
52-
val image: CircleImageView = ItemView.findViewById(R.id.ivBoidPic)
53-
val title: TextView = ItemView.findViewById(R.id.tvBoidTitle)
54-
val subtitle: TextView = ItemView.findViewById(R.id.tvBoidSubtitle)
54+
inner class ViewHolder(binding: BoidItemBinding) : RecyclerView.ViewHolder(binding.root),View.OnClickListener {
55+
val image: CircleImageView = binding.ivBoidPic
56+
val title: TextView = binding.tvBoidTitle
57+
val subtitle: TextView = binding.tvBoidSubtitle
5558

5659
init {
57-
ItemView.setOnClickListener {
58-
val position = adapterPosition
59-
if (position != RecyclerView.NO_POSITION) {
60-
listener.onItemClick(position)
61-
}
62-
}
63-
ItemView.setOnLongClickListener {
60+
binding.clRecentItem.setOnClickListener (this)
61+
binding.clRecentItem.setOnLongClickListener {
6462
val position = adapterPosition
6563
if (position != RecyclerView.NO_POSITION) {
6664
listener.onItemLongClick(position)
6765
}
6866
return@setOnLongClickListener true
6967
}
7068
}
71-
}
7269

73-
interface OnItemClickListener {
74-
fun onItemClick(position: Int)
75-
fun onItemLongClick(position: Int)
70+
override fun onClick(p0: View?) {
71+
val position = adapterPosition
72+
if (position != RecyclerView.NO_POSITION) {
73+
listener.onItemClick(position)
74+
}
75+
}
7676
}
7777
}

app/src/main/res/layout/boid_item.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:id="@+id/cvBoidItem"
54
android:layout_width="match_parent"
65
android:layout_height="@dimen/dp_70"
76
android:layout_marginHorizontal="@dimen/dp_20"

0 commit comments

Comments
 (0)