Kotlin recyclerviewer未显示数据

时间:2018-09-15 20:49:00

标签: android kotlin android-sqlite recycler-adapter

我们的问题是显示来自SQLite database的活动记录。我们能够将数据写入数据库,我们已验证数据是用DBBrowser写入的。此问题的另一部分是RecyclerAdapter。我们花了两天时间来构造语法。程序流程很简单,保存了EditText的一行,然后我们导航到PersonActivity,它应该显示已保存的数据。 在PersonActivity中,我们在数据库中查询所有记录,并返回contactList。我们的设计为MVC,我们将为DBHelperRecyclerAdapterPersonActivity发布以下代码,以显示包含XML文件的数据。还有Model Class

class Contact{
    var id: Int = 0
    var name: String = ""
}

DBHelper插入数据,并且创建queryAll DB不需要所有代码

// Start of CRUD functions
fun insert(values: ContentValues): Long {
    val Id = db!!.insert(dbTable, null, values)
    return Id
}

fun queryAll(): List<Contact> {
    val contactList = ArrayList<Contact>()
    val selectQuery = "SELECT  * FROM $dbTable"
    val cursor = db!!.rawQuery(selectQuery, null)
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                val contact = Contact()
                contact.id = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colId)))
                contact.name = cursor.getString(cursor.getColumnIndex(colName))
                contactList.add(contact)
            } while (cursor.moveToNext())
        }
    }
    cursor.close()
    return contactList
}

RecyclerAdapter 注意注释

class PersonRecyclerAdapter(val context:Context, val 
    items:List<Contact>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 

    RecyclerView.ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.single_card, parent, false))
    }

    override fun getItemCount(): Int {
       return items.size
    }

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        val item = items.get(position)
        viewHolder.bindItem(item)

        //viewHolder.itemName.text = items[position]
        //  WHAT IS CORRECT SYNTAX FOR LINE OF CODE ABOVE
    }
    private fun Any.bindItem(item: Contact) {
        // AS Added this fun ?
    }
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
       val itemName = view.tvName
    }
}

PersonActivity,应显示contactList数据

class PersonActivity : AppCompatActivity() {

    private var rvRecyclerView: RecyclerView? = null
    private var contactList:List<Contact> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_person)

        val names = contactList

        rvRecyclerView?.layoutManager = LinearLayoutManager(this)
        rvRecyclerView?.setHasFixedSize(true)
        rvRecyclerView?.adapter = PersonRecyclerAdapter(this, names)

        callHOME()

    }

    private fun callHOME(){
        val db = DBHelper(this)
        contactList = db.queryAll()

    }
}

PersonActivity的XML文件和膨胀的文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.androidstackoverflow.kotlincontacts.PersonActivity"
    tools:showIn="@layout/activity_person">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rvRecylerView">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

膨胀视图XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_new_card"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="2dp"
    android:background="@color/colorAccent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/color_Black"
        android:textSize="20sp"
        android:text="@string/app_name"
        android:textStyle="bold" />
</LinearLayout>

请理解我们已经针对此问题查看了许多代码变体! 在我们寻求帮助之前。问题有两个方面 如何在PersonActivity上显示数据? PersonRecyclerAdapter语法正确吗?

2 个答案:

答案 0 :(得分:1)

首先,应在初始化名称变量之前调用callHOME()。那就是:

class PersonActivity : AppCompatActivity() {

private var rvRecyclerView: RecyclerView? = null
private var contactList:List<Contact> = ArrayList()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_person)

    callHOME()
    val names = contactList

    rvRecyclerView?.layoutManager = LinearLayoutManager(this)
    rvRecyclerView?.setHasFixedSize(true)
    rvRecyclerView?.adapter = PersonRecyclerAdapter(this, names)
}

private fun callHOME(){
    val db = DBHelper(this)
    contactList = db.queryAll()

} 

您还使用错误的RecyclerView.Adapter<Recycler.ViewHolder>扩展了PersonalRecyclerAdapter类,您必须使用RecyclerView.Adapter<MyViewHolder>。 使用自己的ViewHolder作为参数的RecyclerView.Adapter,而不是默认视图持有者。

在这里使用扩展功能是多余的和不必要的。虽然可以使用如下。.

class PersonRecyclerAdapter(val context:Context, val 
    items:List<Contact>):RecyclerView.Adapter<MyViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
MyViewHolder {
     return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.single_card,parent, false)) 
    }

    override fun getItemCount(): Int {
       return items.size
    }

    override fun onBindViewHolder(viewHolder: MyViewHolder,    position: 
    Int) {
      val item = items.get(position)
      viewHolder.bindItem(item)
    }

    private fun MyViewHolder.bindItem(item: Contact) {
       itemName.text = item.name
       // or use
       // itemView.findViewById<TextView>(R.id.tvName).text = item.name
    }


}

class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val itemName = view.tvName
}

相反,只需使用此

 class PersonRecyclerAdapter(val context:Context, val 
    items:List<Contact>):RecyclerView.Adapter<MyViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 
   MyViewHolder { return MyViewHolder(LayoutInflater.from(context).inflate(R.layout.single_card, parent, false)) }

   override fun getItemCount(): Int {
       return items.size
    }

    override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
        val item = items.get(position)
        viewHolder.itemName.text = items[position].name
        // or use
        // viewHolder.itemView.findViewById<TextView>(R.id.tvName).text = item.name
    }


}

class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {

     val itemName = view.tvName
 }

答案 1 :(得分:0)

首先,@ kumarshivang就如何构造RecyclerAdapter提出了一些非常好的建议,他的最佳点是不使用内部类。因此,我将在此处发布针对RecyclerAdapter使用更简洁代码设计的代码。尽管即使使用RecyclerAdapter更好的构造,也无法解决我们看不到存储数据的问题。因为我们知道数据在数据库逻辑中,所以会告诉我们两个错误:要么数据没有容器来显示视图,要么数据不可用。YEP是后者,而{{1}中的这一行代码}是丢失的内容。

PersonActivity

这里是清洁工listItems = db.queryAll() (关闭我们要去的FACTORY FACTORY)
请注意,作者使用RecyclerAdapter小部件使用“卡”一词在CardView小部件中显示数据。

RecyclerView