Commit 9d2a7e51 by Paktalin

Set up RecyclerView

parent fc1ee8d4
......@@ -23,6 +23,9 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'com.android.support:support-annotations:28.0.0-rc02'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'com.google.firebase:firebase-core:16.0.3'
......@@ -31,7 +34,6 @@ dependencies {
implementation 'com.firebase:firebase-client-android:2.3.1'
implementation 'com.google.firebase:firebase-firestore:17.1.0'
implementation 'com.android.support:support-annotations:28.0.0-rc02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
......
package com.paktalin.vocabularynotebook;
import android.support.annotation.NonNull;
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;
import java.util.List;
public class VocabularyAdapter extends RecyclerView.Adapter<VocabularyAdapter.ViewHolder> {
private static final String TAG = "VN/" + VocabularyAdapter.class.getSimpleName();
private List<WordItemPojo> wordItems;
public VocabularyAdapter(List<WordItemPojo> wordItems) {
this.wordItems = wordItems;
Log.d(TAG, "wordItems: " + wordItems);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
WordItemPojo item = wordItems.get(position);
Log.d(TAG, "bind item: " + item);
holder.tvWord.setText(item.getWord());
holder.tvTranslation.setText(item.getTranslation());
}
@Override
public int getItemCount() {
return wordItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvWord, tvTranslation;
ViewHolder(View itemView) {
super(itemView);
tvWord = itemView.findViewById(R.id.tvWord);
tvTranslation = itemView.findViewById(R.id.tvTranslation);
}
}
}
......@@ -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.WordPojo
import com.paktalin.vocabularynotebook.pojo.WordItemPojo
import kotlinx.android.synthetic.main.activity_add_word.*
class AddWordActivity : AppCompatActivity() {
......@@ -35,7 +35,7 @@ class AddWordActivity : AppCompatActivity() {
val translation = etTranslation.text.toString()
if (Utils.fieldsNotEmpty(word, translation, "Please, enter word and translation", this)) {
db.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(WordPojo(word, translation)).addOnSuccessListener {
.collection(WORDS).add(WordItemPojo(word, translation)).addOnSuccessListener {
Log.i(TAG, "Successfully added a new word $word")
clearFields()
}
......
......@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
......@@ -13,13 +14,21 @@ import com.google.firebase.firestore.DocumentReference
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 kotlinx.android.synthetic.main.fragment_vocabulary.*
import java.util.*
class VocabularyFragment : Fragment() {
companion object {
private val TAG = "VN/" + VocabularyFragment::class.simpleName
private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words"
}
private lateinit var userDocument: DocumentReference
private val db = FirebaseFirestore.getInstance()
private lateinit var vocabularyId: String
private lateinit var vocabulary: DocumentReference
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
......@@ -46,7 +55,7 @@ class VocabularyFragment : Fragment() {
private fun setVocabularyId(task: DocumentSnapshot) {
//todo if only one vocabulary exists, open it
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
vocabularyId = vocabularies[0].id
vocabulary = db.collection(VOCABULARIES).document(vocabularies[0].id)
}
@SuppressLint("SetTextI18n")
......@@ -62,18 +71,35 @@ class VocabularyFragment : Fragment() {
private fun retrieveVocabularyData() {
//todo if only one vocabulary exists, open it
db.collection("vocabularies").document(vocabularyId).get().addOnSuccessListener { task ->
val vocabTitle = task.get("title").toString()
tvUserData.append("\n\nvocabularies:\n$vocabTitle")
vocabulary.get().addOnSuccessListener { task ->
val vocabularyTitle = task.get("title").toString()
tvUserData.append("\n\nvocabularies:\n$vocabularyTitle")
}
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
recyclerView.setHasFixedSize(true)
val items: List<WordItemPojo> = Arrays.asList(
WordItemPojo("uno", "one"),
WordItemPojo("due", "two"),
WordItemPojo("tre", "three"))
val adapter = VocabularyAdapter(items)
recyclerView.adapter = adapter
/*vocabulary.collection(WORDS).get().addOnSuccessListener {
val items: List<String> = Arrays.asList("one", "two", "three")
val adapter = VocabularyAdapter(items)
recyclerView.adapter = adapter
}*/
}
private fun addWord() {
val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabularyId)
addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent)
}
companion object { private val TAG = "VN/" + VocabularyFragment::class.simpleName }
private fun retrieveWordsFromVocabulary() {
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.pojo;
public class WordPojo {
public class WordItemPojo {
private String word, translation;
public WordPojo(String word, String translation) {
public WordItemPojo(String word, String translation) {
this.word = word;
this.translation = translation;
}
......
......@@ -36,8 +36,9 @@
app:layout_constraintTop_toBottomOf="@+id/tvCongrats" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="200dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/tvUserData" />
......
<?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="wrap_content">
<TextView
android:id="@+id/tvWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="word" />
<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
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