Commit a73ceeb5 by Paktalin

word_item is now in table layout

parent ae7ee12b
package com.paktalin.vocabularynotebook
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.paktalin.vocabularynotebook.pojo.WordItemPojo
class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val wordItems: List<WordItem>) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
......@@ -19,8 +16,8 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = wordItems[position]
holder.tvWord.text = item.word
holder.tvTranslation.text = item.translation
holder.tvWord.text = item.pojo.word
holder.tvTranslation.text = item.pojo.translation
}
override fun getItemCount(): Int {
......@@ -30,7 +27,6 @@ class VocabularyAdapter(private val wordItems: List<WordItemPojo>) : RecyclerVie
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvWord: TextView = itemView.findViewById(R.id.tvWord)
var tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation)
}
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) {
this.word = word;
this.translation = translation;
}
public WordItemPojo(String word, String translation) {
this.word = word;
this.translation = translation;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getWord() {
return word;
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
}
public void setWord(String word) {
this.word = word;
private String wordItemId;
private WordItemPojo pojo;
public WordItem(String word, String translation, String wordItemId) {
this.pojo = new WordItemPojo(word, translation);
this.wordItemId = wordItemId;
}
public String getTranslation() {
return translation;
public WordItemPojo getPojo() {
return pojo;
}
public void setTranslation(String translation) {
this.translation = translation;
public void setPojo(WordItemPojo pojo) {
this.pojo = pojo;
}
}
......@@ -9,7 +9,7 @@ import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
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.*
class AddWordActivity : AppCompatActivity() {
......
......@@ -13,7 +13,8 @@ import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
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.*
class VocabularyFragment : Fragment() {
......@@ -28,6 +29,8 @@ class VocabularyFragment : Fragment() {
private val db = FirebaseFirestore.getInstance()
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? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
}
......@@ -63,20 +66,9 @@ class VocabularyFragment : Fragment() {
//todo if only one vocabulary exists, open it
val mLayoutManager = LinearLayoutManager(activity)
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)
recyclerView.adapter = adapter
}
vocabulary.collection(WORDS).get()
.addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
private fun addWord() {
......@@ -84,4 +76,20 @@ class VocabularyFragment : Fragment() {
addWordIntent.putExtra("vocabularyId", vocabulary.id)
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 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
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:id="@+id/recyclerView"
android:layout_width="match_parent"
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<TableLayout
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_height="wrap_content">
android:layout_height="wrap_content"
android:stretchColumns="0, 1">
<TextView
android:id="@+id/tvWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="word" />
<TableRow android:padding="8dp">
<TextView
android:id="@+id/tvTranslation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="translation"
app:layout_constraintStart_toEndOf="@+id/tvWord" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<TextView
android:id="@+id/tvWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="word"
android:textSize="22sp" />
<TextView
android:id="@+id/tvTranslation"
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