MAIN ACTIVITY
package com.andela.smithnwokocha.firebaselistview import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.EditText import android.widget.ListView import android.widget.TextView import com.firebase.ui.database.FirebaseListAdapter import com.google.firebase.database.FirebaseDatabase class MainActivity : AppCompatActivity() { private val db = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://listviewtest-338e0.firebaseio.com/users") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val btn=findViewById<View>(R.id.button2) as Button val enter=findViewById<View>(R.id.editText) as EditText // val ref = db val mlist=findViewById<View>(R.id.list) as ListView val adapter = object : FirebaseListAdapter<String>
(this,String::class.java,R.layout.row,db) { override fun populateView(v: View?, model: String?, position: Int) { val text = v?.findViewById(R.id.textView) as TextView text.text = model } } mlist.adapter=adapter btn.setOnClickListener { val ad=enter.text.toString().trim() enter.text=null db.push().setValue(ad) } } }
MAIN XML
<?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"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andela.smithnwokocha.firebaselistview.MainActivity"> <EditText android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="90dp"
android:layout_marginLeft="16dp"
android:drawableLeft="@drawable/ic_mood"
android:ems="10"
android:hint="Type Your Message"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent" /> <Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginStart="2dp"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText" /> <ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="445dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:listSelector="@android:color/transparent"
android:transcriptMode="alwaysScroll"
android:divider="@null"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
ROW.XML
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:orientation="vertical"
android:paddingBottom="10dp"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:maxWidth="100dp"
android:padding="5dp"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout></android.support.constraint.ConstraintLayout>
FIREBASE UI DEPENDENCIES
implementation 'com.google.firebase:firebase-database:10.0.0'
implementation 'com.firebaseui:firebase-ui-database:0.6.2'
not work
ReplyDeleteNeither an Android cell phone nor Android tablets can help in finishing fill in as effortlessly as PCs. fallout 4 advlevel
ReplyDeleteAwesome post, and incredible site. A debt of gratitude is in order for the data! chat avenue
ReplyDelete