Commit a73ceeb5 by Paktalin

word_item is now in table layout

parent ae7ee12b
package com.paktalin.vocabularynotebook package com.paktalin.vocabularynotebook
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import com.paktalin.vocabularynotebook.pojo.WordItemPojo class VocabularyAdapter(private val wordItems: List<WordItem>) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context) val view = LayoutInflater.from(parent.context)
...@@ -19,8 +16,8 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie ...@@ -19,8 +16,8 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = wordItems[position] val item = wordItems[position]
holder.tvWord.text = item.word holder.tvWord.text = item.pojo.word
holder.tvTranslation.text = item.translation holder.tvTranslation.text = item.pojo.translation
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
...@@ -30,7 +27,6 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie ...@@ -30,7 +27,6 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvWord: TextView = itemView.findViewById(R.id.tvWord) var tvWord: TextView = itemView.findViewById(R.id.tvWord)
var tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation) var tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation)
} }
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName } companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
......
package com.paktalin.vocabularynotebook.pojo; package com.paktalin.vocabularynotebook;
public class WordItemPojo { public class WordItem {
public static class WordItemPojo {
private String word, translation; private String word, translation;
...@@ -24,4 +25,21 @@ public class WordItemPojo { ...@@ -24,4 +25,21 @@ public class WordItemPojo {
public void setTranslation(String translation) { public void setTranslation(String translation) {
this.translation = translation; this.translation = translation;
} }
}
private String wordItemId;
private WordItemPojo pojo;
public WordItem(String word, String translation, String wordItemId) {
this.pojo = new WordItemPojo(word, translation);
this.wordItemId = wordItemId;
}
public WordItemPojo getPojo() {
return pojo;
}
public void setPojo(WordItemPojo pojo) {
this.pojo = pojo;
}
} }
...@@ -9,7 +9,7 @@ import com.google.firebase.firestore.FirebaseFirestore ...@@ -9,7 +9,7 @@ import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.pojo.WordItemPojo import com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import kotlinx.android.synthetic.main.activity_add_word.* import kotlinx.android.synthetic.main.activity_add_word.*
class AddWordActivity : AppCompatActivity() { class AddWordActivity : AppCompatActivity() {
......
...@@ -13,7 +13,8 @@ import com.google.firebase.firestore.DocumentSnapshot ...@@ -13,7 +13,8 @@ import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.VocabularyAdapter import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.pojo.WordItemPojo import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import kotlinx.android.synthetic.main.fragment_vocabulary.* import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() { class VocabularyFragment : Fragment() {
...@@ -28,6 +29,8 @@ class VocabularyFragment : Fragment() { ...@@ -28,6 +29,8 @@ class VocabularyFragment : Fragment() {
private val db = FirebaseFirestore.getInstance() private val db = FirebaseFirestore.getInstance()
private lateinit var vocabulary: DocumentReference private lateinit var vocabulary: DocumentReference
//todo move data process to onCreate method and update the views later
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false) return inflater.inflate(R.layout.fragment_vocabulary, container, false)
} }
...@@ -63,25 +66,30 @@ class VocabularyFragment : Fragment() { ...@@ -63,25 +66,30 @@ class VocabularyFragment : Fragment() {
//todo if only one vocabulary exists, open it //todo if only one vocabulary exists, open it
val mLayoutManager = LinearLayoutManager(activity) val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager recyclerView.layoutManager = mLayoutManager
recyclerView.setHasFixedSize(true)
vocabulary.collection(WORDS).get().addOnSuccessListener { vocabulary.collection(WORDS).get()
val wordItems: MutableList<WordItemPojo> = mutableListOf() .addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
private fun addWord() {
val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent)
}
for (ref in it.documents) { private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) {
val wordItems: MutableList<WordItem> = mutableListOf()
for (ref in documents) {
val word = ref.get("word").toString() val word = ref.get("word").toString()
val translation = ref.get("translation").toString() val translation = ref.get("translation").toString()
wordItems.add(WordItemPojo(word, translation)) val wordItemId = ref.id
wordItems.add(WordItem(word, translation, wordItemId))
} }
val adapter = VocabularyAdapter(wordItems) val adapter = VocabularyAdapter(wordItems)
recyclerView.adapter = adapter recyclerView.adapter = adapter
}
}
private fun addWord() { //todo setOnItemClickListener
val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent)
} }
} }
\ No newline at end of file
...@@ -4,10 +4,15 @@ ...@@ -4,10 +4,15 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@android:color/transparent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.paktalin.vocabularynotebook.activities.VocabularyFragment"> tools:context="com.paktalin.vocabularynotebook.activities.VocabularyFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background_small"
android:scaleType="centerCrop"/>
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView" android:id="@+id/recyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout <TableLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:stretchColumns="0, 1">
<TableRow android:padding="8dp">
<TextView <TextView
android:id="@+id/tvWord" android:id="@+id/tvWord"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="word" /> android:text="word"
android:textSize="22sp" />
<TextView <TextView
android:id="@+id/tvTranslation" android:id="@+id/tvTranslation"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="translation" android:text="translation"
app:layout_constraintStart_toEndOf="@+id/tvWord" /> android:textSize="22sp" />
</android.support.constraint.ConstraintLayout> </TableRow>
\ No newline at end of file </TableLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment