Commit 53ad7bb7 by Paktalin

Word info is opened on click

parent a73ceeb5
......@@ -24,6 +24,7 @@
</intent-filter>
</activity>
<activity android:name=".activities.AddWordActivity"/>
<activity android:name=".activities.WordItemInfoActivity" />
</application>
</manifest>
\ No newline at end of file
package com.paktalin.vocabularynotebook
import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.paktalin.vocabularynotebook.activities.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: List<WordItem>) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val wordItems: List<WordItem>,
private val context: Context) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
......@@ -15,9 +19,10 @@ class VocabularyAdapter(private val wordItems: List<WordItem>) : RecyclerView.Ad
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = wordItems[position]
holder.tvWord.text = item.pojo.word
holder.tvTranslation.text = item.pojo.translation
val wordItem = wordItems[position]
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.tvWord.setOnClickListener { openWordItemInfo(wordItem) }
}
override fun getItemCount(): Int {
......@@ -25,8 +30,14 @@ class VocabularyAdapter(private val wordItems: List<WordItem>) : RecyclerView.Ad
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tvWord: TextView = itemView.findViewById(R.id.tvWord)
var tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation)
val tvWord: TextView = itemView.findViewById(R.id.tvWord)
val tvTranslation: TextView = itemView.findViewById(R.id.tvTranslation)
}
private fun openWordItemInfo(wordItem: WordItem) {
val intentWordItemInfo = Intent(context, WordItemInfoActivity::class.java)
intentWordItemInfo.putExtra("wordItem", wordItem)
context.startActivity(intentWordItemInfo)
}
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
......
package com.paktalin.vocabularynotebook;
public class WordItem {
public static class WordItemPojo {
import java.io.Serializable;
public class WordItem implements Serializable {
public static class WordItemPojo implements Serializable{
private String word, translation;
......@@ -27,12 +29,12 @@ public class WordItem {
}
}
private String wordItemId;
private String id;
private WordItemPojo pojo;
public WordItem(String word, String translation, String wordItemId) {
public WordItem(String word, String translation, String id) {
this.pojo = new WordItemPojo(word, translation);
this.wordItemId = wordItemId;
this.id = id;
}
public WordItemPojo getPojo() {
......@@ -42,4 +44,12 @@ public class WordItem {
public void setPojo(WordItemPojo pojo) {
this.pojo = pojo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
......@@ -14,7 +14,6 @@ import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import kotlinx.android.synthetic.main.fragment_vocabulary.*
class VocabularyFragment : Fragment() {
......@@ -37,6 +36,7 @@ class VocabularyFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setEmptyAdapter()
fabAddWord.setOnClickListener( { addWord() } )
retrieveData()
}
......@@ -46,6 +46,13 @@ class VocabularyFragment : Fragment() {
FirebaseAuth.getInstance().signOut()
}
private fun setEmptyAdapter() {
val emptyList:MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!)
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
}
private fun retrieveData() {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
userDocument = db.collection(USERS).document(userId)
......@@ -64,9 +71,6 @@ class VocabularyFragment : Fragment() {
private fun retrieveVocabularyData() {
//todo if only one vocabulary exists, open it
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
vocabulary.collection(WORDS).get()
.addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
......@@ -87,9 +91,7 @@ class VocabularyFragment : Fragment() {
wordItems.add(WordItem(word, translation, wordItemId))
}
val adapter = VocabularyAdapter(wordItems)
val adapter = VocabularyAdapter(wordItems, activity!!)
recyclerView.adapter = adapter
//todo setOnItemClickListener
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.activities
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.activity_word_info.*
class WordItemInfoActivity: AppCompatActivity() {
private lateinit var wordItem: WordItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_word_info)
wordItem = intent.getSerializableExtra("wordItem") as WordItem
updateUi()
}
private fun updateUi() {
tvWord.text = wordItem.pojo.word
tvTranslation.text = wordItem.pojo.translation
}
companion object { private val TAG = "VN/" + WordItemInfoActivity::class.java.simpleName }
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="match_parent">
<TextView
android:id="@+id/tvWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Word"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvTranslation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Translation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvWord" />
</android.support.constraint.ConstraintLayout>
\ 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