今やっていること
Kotlinで掲示板アプリを作っています。
Firestoreから、Recycleviewにデータを読み取って画面に表示させたいと思っています。
いろいろなサイトを参考にしながら進めてきましたが、分からないところがでてきました。
Firestoreから読み取りたいものは
・名前
・投稿した時間
・タイトル
・詳細
・(アイコン)←まだFirestorageに保存していない
質問したいこと
たくさんあってすみません。
・データクラスは必要か
・データクラスの、投稿時間「var createAt: Date = Date(System.currentTimeMillis()),」はあっているのか。また、必要か。
・CustomAdapterクラスをどこで継承するのか
・他にも意味のないコードがあったり、必要なコードがあったら教えていただきたいです。
コード
1行分のレイアウト
item_post.xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="150dp" 4 android:clickable="true" 5 android:focusable="true" 6 android:padding="12dp"> 7 8 <de.hdodenhof.circleimageview.CircleImageView 9 android:id="@+id/icon" 10 android:layout_width="60dp" 11 android:layout_height="60dp" /> 12 13 <LinearLayout 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_marginStart="16dp" 17 android:layout_weight="1" 18 android:orientation="vertical"> 19 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:orientation="horizontal"> 24 25 <TextView 26 android:id="@+id/tvname" 27 android:layout_width="0dp" 28 android:layout_height="wrap_content" 29 android:layout_weight="1" 30 android:textSize="20sp" 31 android:textStyle="bold" 32 android:layout_margin="5dp"/> 33 34 <TextView 35 android:id="@+id/tvtime" 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:textSize="20sp" 40 android:layout_margin="5dp"/> 41 42 </LinearLayout> 43 44 <TextView 45 android:id="@+id/tvtitle" 46 android:layout_width="match_parent" 47 android:layout_height="wrap_content" 48 android:layout_marginBottom="4dp" 49 android:textColor="#000000" 50 android:textSize="34sp" 51 android:textStyle="bold"/> 52 53 <TextView 54 android:id="@+id/tvpost" 55 android:layout_width="match_parent" 56 android:layout_height="wrap_content" 57 android:textColor="#000000" 58 android:textSize="25sp" /> 59 60 </LinearLayout> 61</LinearLayout>
データクラス
Post.kt
1package com.example.a.Post 2 3import java.sql.Date 4 5data class Post( 6 val icon: Int, 7 val title: String, 8 val detail: String, 9 var createAt: Date = Date(System.currentTimeMillis()), 10)
アダプター
CustomAdapter.kt
1package com.example.a.Post 2 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.ImageView 7import android.widget.TextView 8import androidx.recyclerview.widget.RecyclerView 9import com.example.a.R 10 11class CustomAdapter(private val postlist: ArrayList<Post>) : 12 RecyclerView.Adapter<CustomAdapter.ViewHolder>() { 13 14 class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { 15 16 val icon: ImageView 17 val title: TextView 18 val post: TextView 19 20 init { 21 icon = view.findViewById(R.id.icon) 22 title = view.findViewById(R.id.tvtitle) 23 post = view.findViewById(R.id.tvpost) 24 } 25 } 26 27 //レイアウトの設定 28 override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { 29 val view = 30 LayoutInflater.from(viewGroup.context).inflate(R.layout.item_post, viewGroup, false) 31 return ViewHolder(view) 32 } 33 34 //Viewの設定 35 override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { 36 val post = postlist[position] 37 38 viewHolder.icon.setImageResource(post.icon) 39 viewHolder.title.text = post.title 40 viewHolder.post.text = post.detail 41 } 42 43 //表示数を返す 44 override fun getItemCount(): Int = postlist.size 45}
表示させるフラグメント
HomeFragment.kt
1package com.example.a.Home 2 3import android.content.ContentValues.TAG 4import android.content.Intent 5import androidx.lifecycle.ViewModelProvider 6import android.os.Bundle 7import android.util.Log 8import androidx.fragment.app.Fragment 9import android.view.LayoutInflater 10import android.view.View 11import android.view.ViewGroup 12import com.example.a.Post.CustomAdapter 13import com.example.a.Post.PostScreenActivity 14import com.example.a.databinding.HomeFragmentBinding 15import com.google.firebase.auth.ktx.auth 16import com.google.firebase.firestore.CollectionReference 17import com.google.firebase.firestore.Query 18import com.google.firebase.firestore.ktx.firestore 19import com.google.firebase.ktx.Firebase 20 21class HomeFragment : Fragment() { 22 23 companion object { 24 fun newInstance() = HomeFragment() 25 } 26 27 private lateinit var viewModel: HomeViewModel 28 private var _binding: HomeFragmentBinding? = null 29 private val binding get() = _binding!! 30 31 override fun onCreateView( 32 inflater: LayoutInflater, container: ViewGroup?, 33 savedInstanceState: Bundle? 34 ): View? { 35 _binding = HomeFragmentBinding.inflate(inflater, container, false) 36 val view = binding.root 37 return view 38 } 39 40 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 41 super.onViewCreated(view, savedInstanceState) 42 binding.btnpostsc.setOnClickListener { 43 startActivity( 44 Intent( 45 context, 46 PostScreenActivity::class.java 47 ) 48 ) 49 } 50 51 val db = Firebase.firestore 52 val user = Firebase.auth.currentUser 53 val uid = user!!.uid 54 55 val citiesRef = db.collection("users").document(uid).collection("UserPosts") 56 citiesRef.orderBy("createdDay", Query.Direction.DESCENDING) 57 citiesRef.get() 58 .addOnSuccessListener { Log.d(TAG, "読み取り成功") } 59 .addOnFailureListener { Log.d(TAG, "読み取り失敗") } 60 } 61 62 63 override fun onActivityCreated(savedInstanceState: Bundle?) { 64 super.onActivityCreated(savedInstanceState) 65 viewModel = ViewModelProvider(this).get(HomeViewModel::class.java) 66 // TODO: Use the ViewModel 67 } 68 69 70 override fun onDestroyView() { 71 super.onDestroyView() 72 _binding = null 73 } 74 75}
参考にしたサイト
RecyclerViewでグリッド表示を実装する方法を解説
https://hirauchi-genta.com/kotlin-view-recycerview-grid/
あなたの回答
tips
プレビュー