Bonjour à tous,

Je suis bloqué depuis plusieurs jours maintenant, j'essaye de mettre en place un système de favoris sur mon application.

J'utilise l'API : NewsAPI, et j'ai 2 fragments, un "home" qui ressence les derniers articles et un "dashboard" qui devra ressencer les articles mis en favoris grace à mon image bookmark, qui lorsque je clique dessus copie l'article dans l'onglet Dashboard.

Mais je n'arrive pas à faire cela, j'ai donc besoin de votre aide.

J'ai un deuxième soucis, lorsque je suis sur le premier onglet / fragment, et que je change puis reviens sur le fragment principal mon appli crash, je sais pas si quelqu'un aurait une idée pourquoi ça fait cela.

Voici le code de mon ArticleAdapter.kt :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class ArticleAdapter(private val context: Context, private var articleList: List<Article>) : RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder>() {
 
    private var isBookmarked = false
    private val favoriteArticleList = ArrayList<Article>()
    private var onItemClickListener: OnItemClickListener? = null
 
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_layout, parent, false)
 
        return ArticleViewHolder(view)
    }
 
    override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
        val article = articleList[position]
        holder.bookmarkImageView.setImageResource(if (favoriteArticleList.contains(article)) R.drawable.baseline_bookmark_24 else R.drawable.baseline_bookmark_border_24)
 
        holder.bind(article)
        holder.shareImageView.setOnClickListener {
            showSharePopupMenu(holder.bookmarkImageView, article)
        }
 
        holder.bookmarkImageView.setOnClickListener {
            holder.bookmarkImageView.setImageResource(if (favoriteArticleList.contains(article)) R.drawable.baseline_bookmark_24 else R.drawable.baseline_bookmark_border_24)
 
            if (favoriteArticleList.contains(article))
            {
                Toast.makeText(context, "Removed from bookmark", Toast.LENGTH_SHORT).show()
                favoriteArticleList.remove(article)
            }
            else
            {
                Toast.makeText(context, "Added to bookmark", Toast.LENGTH_SHORT).show()
                favoriteArticleList.add(article)
            }
            notifyDataSetChanged()
        }
 
        holder.itemView.setOnClickListener {
            onItemClickListener?.onItemClick(article)
        }
    }
 
    override fun getItemCount(): Int {
        return articleList.size
    }
 
    fun updateData(newArticleList: List<Article>) {
        articleList = newArticleList
        notifyDataSetChanged()
    }
 
    inner class ArticleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val bookmarkImageView: ImageView = itemView.findViewById(R.id.imageView_bookmark)
        val shareImageView: ImageView = itemView.findViewById(R.id.imageView_share)
        private val titleTextView: TextView = itemView.findViewById(R.id.textView_title)
        private val dateTextView: TextView = itemView.findViewById(R.id.textView_date)
        private val descriptionTextView: TextView = itemView.findViewById(R.id.textView_description)
        private val articleImageView: ImageView = itemView.findViewById(R.id.imageView)
 
        fun bind(article: Article) {
            titleTextView.text = article.title
            dateTextView.text = article.publishedAt.replace("T", " ").replace("Z", "")
            descriptionTextView.text = article.description
 
            Glide.with(itemView.context)
                .load(article.urlToImage)
                .into(articleImageView)
        }
    }
 
    private fun showSharePopupMenu(anchorView: View, article: Article) {
        shareContent(article.url)
    }
 
    private fun shareContent(content: String) {
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "text/plain"
        shareIntent.putExtra(Intent.EXTRA_TEXT, content)
        context.startActivity(Intent.createChooser(shareIntent, "Partager via"))
    }
 
    interface OnItemClickListener {
        fun onItemClick(article: Article)
    }
 
    fun setOnItemClickListener(listener: OnItemClickListener) {
        onItemClickListener = listener
    }
}
Et celui de mon DashboardFragment.kt :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class DashboardFragment : Fragment() {
 
    private var _binding: FragmentDashboardBinding? = null
    private lateinit var recyclerView: RecyclerView
    private var favoriteArticlesList = mutableListOf<Article>()
    private lateinit var articleAdapter: ArticleAdapter
    private lateinit var favoriteArticleAdapter: ArticleAdapter // Ajout de l'adaptateur pour les articles favoris
 
 
 
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
 
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        val dashboardViewModel =
                ViewModelProvider(this).get(DashboardViewModel::class.java)
 
        _binding = FragmentDashboardBinding.inflate(inflater, container, false)
        val root: View = binding.root
 
        recyclerView = root.findViewById(R.id.recyclerViewDashboard)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        articleAdapter = ArticleAdapter(requireContext(), emptyList())
        favoriteArticleAdapter = ArticleAdapter(requireContext(), emptyList())
        recyclerView.adapter = favoriteArticleAdapter
 
        favoriteArticlesList = mutableListOf() // Initialisation de la liste des articles favoris
 
        articleAdapter.setOnItemClickListener(object : ArticleAdapter.OnItemClickListener {
            override fun onItemClick(article: Article) {
                if (favoriteArticlesList.contains(article)) {
                    favoriteArticlesList.remove(article)
                } else {
                    favoriteArticlesList.add(article)
                }
                favoriteArticleAdapter.updateData(favoriteArticlesList)
            }
        })
 
        return root
    }
 
    override fun onResume() {
        super.onResume()
        articleAdapter.notifyDataSetChanged()
        favoriteArticleAdapter.notifyDataSetChanged()
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Pour ce qui est des xml, les 2 fragments on le même xml avec un recyclerView et une barre de recherche (juste les id changent) :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.DashboardFragment">
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewDashboard"
        android:layout_width="match_parent"
        android:layout_height="666dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/search"
        app:layout_constraintVertical_bias="0.647" />
 
    <androidx.appcompat.widget.SearchView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:id="@+id/search"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:iconifiedByDefault="false"
        app:searchHintIcon="@null"
        app:queryHint="Search..."
        android:focusable="false"
        app:closeIcon="@drawable/baseline_clear_24"
        app:searchIcon="@drawable/baseline_search_24" />
 
</androidx.constraintlayout.widget.ConstraintLayout>
Et j'ai un list_item_layout.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginHorizontal="10dp"
    android:layout_marginVertical="10dp"
    android:clickable="true"
    android:focusable="true"
    app:cardCornerRadius="20dp"
    app:cardElevation="8dp">
 
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <LinearLayout
            android:id="@+id/titleLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0">
 
            <TextView
                android:id="@+id/textView_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Titre"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:layout_gravity="start"
                android:gravity="start"
                android:paddingStart="10dp"
                android:paddingEnd="40dp" />
 
            <ImageView
                android:id="@+id/imageView_bookmark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="16dp"
                android:background="@android:color/transparent"
                android:clickable="true"
                android:src="@drawable/baseline_bookmark_border_24"
                app:layout_constraintBottom_toTopOf="@+id/imageView"
                app:layout_constraintEnd_toEndOf="@+id/textView_description"
                app:layout_constraintTop_toBottomOf="@+id/textView_description"
                app:layout_constraintVertical_bias="0.0" />
 
        </LinearLayout>
 
        <TextView
            android:id="@+id/textView_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textColor="@android:color/holo_red_dark"
            android:textSize="12sp"
            android:layout_marginStart="10dp"
            app:layout_constraintStart_toStartOf="@+id/titleLayout"
            app:layout_constraintTop_toBottomOf="@+id/titleLayout"
            app:layout_constraintEnd_toEndOf="@+id/titleLayout"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.0" />
 
        <TextView
            android:id="@+id/textView_description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="16sp"
            android:layout_marginStart="10dp"
            app:layout_constraintStart_toStartOf="@+id/titleLayout"
            app:layout_constraintTop_toBottomOf="@+id/textView_date"
            app:layout_constraintEnd_toEndOf="@+id/titleLayout"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.0"
            android:layout_marginBottom="8dp" />
 
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="350dp"
            android:layout_height="250dp"
            app:layout_constraintStart_toStartOf="@+id/titleLayout"
            app:layout_constraintTop_toBottomOf="@+id/imageView_share"
            app:layout_constraintEnd_toEndOf="@+id/titleLayout"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintVertical_bias="0.0" />
 
        <ImageView
            android:id="@+id/imageView_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:src="@drawable/baseline_share_24"
            app:layout_constraintBottom_toTopOf="@+id/imageView"
            app:layout_constraintEnd_toEndOf="@+id/textView_description"
            app:layout_constraintTop_toBottomOf="@+id/textView_description"
            app:layout_constraintVertical_bias="0.0" />
 
    </androidx.constraintlayout.widget.ConstraintLayout>
 
</androidx.cardview.widget.CardView>

Hésitez pas à me dire s'il vous faut d'autres parties de mon code.

Merci d'avance.

Zapsalis