Populate listview using kotlin and firebase listview adapter - Joklinz-Tech

Latest

Home Of Tech news,jobs and android tutorials

Saturday, 3 February 2018

Populate listview using kotlin and firebase listview adapter





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="368dp"
        android:layout_height="445dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp" 
       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">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="18sp" 
       android:textStyle="bold" 
       app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

main activity

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 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
    }
}

1 comment:

  1. I've got error "Unresolved reference: FirebaseListAdapter", how can i fix that?

    ReplyDelete