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;
public WordItemPojo(String word, String translation) { public WordItemPojo(String word, String translation) {
this.word = word; this.word = word;
this.translation = translation; this.translation = translation;
} }
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getWord() { public String getTranslation() {
return word; return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
} }
public void setWord(String word) { private String wordItemId;
this.word = word; private WordItemPojo pojo;
public WordItem(String word, String translation, String wordItemId) {
this.pojo = new WordItemPojo(word, translation);
this.wordItemId = wordItemId;
} }
public String getTranslation() { public WordItemPojo getPojo() {
return translation; return pojo;
} }
public void setTranslation(String translation) { public void setPojo(WordItemPojo pojo) {
this.translation = translation; 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,20 +66,9 @@ class VocabularyFragment : Fragment() { ...@@ -63,20 +66,9 @@ 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 {
val wordItems: MutableList<WordItemPojo> = mutableListOf()
for (ref in it.documents) {
val word = ref.get("word").toString()
val translation = ref.get("translation").toString()
wordItems.add(WordItemPojo(word, translation))
}
val adapter = VocabularyAdapter(wordItems) vocabulary.collection(WORDS).get()
recyclerView.adapter = adapter .addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
} }
private fun addWord() { private fun addWord() {
...@@ -84,4 +76,20 @@ class VocabularyFragment : Fragment() { ...@@ -84,4 +76,20 @@ class VocabularyFragment : Fragment() {
addWordIntent.putExtra("vocabularyId", vocabulary.id) addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent) startActivity(addWordIntent)
} }
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) {
val wordItems: MutableList<WordItem> = mutableListOf()
for (ref in documents) {
val word = ref.get("word").toString()
val translation = ref.get("translation").toString()
val wordItemId = ref.id
wordItems.add(WordItem(word, translation, wordItemId))
}
val adapter = VocabularyAdapter(wordItems)
recyclerView.adapter = adapter
//todo setOnItemClickListener
}
} }
\ 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">
<TextView <TableRow android:padding="8dp">
android:id="@+id/tvWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="word" />
<TextView <TextView
android:id="@+id/tvTranslation" 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:layout_marginLeft="8dp" android:text="word"
android:layout_marginStart="8dp" android:textSize="22sp" />
android:text="translation"
app:layout_constraintStart_toEndOf="@+id/tvWord" /> <TextView
</android.support.constraint.ConstraintLayout> android:id="@+id/tvTranslation"
\ No newline at end of file android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translation"
android:textSize="22sp" />
</TableRow>
</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