回收者视图显示空白

时间:2018-08-03 13:08:31

标签: android android-recyclerview kotlin

我试图显示页面供稿或用户在应用程序内发布供稿,但未获得所需的输出。而不是发布消息,我得到了一个空的活动,并怀疑错误是在适配器类内部。

recylerView = findViewById<RecyclerView>(R.id.recyclerView)
var inBundle: Bundle = getIntent().getExtras()
var araayList: ArrayList<Data> = inBundle.getParcelableArrayList<Data>("data")

if (araayList == null) {
    Toast.makeText(this, "it's empty", Toast.LENGTH_SHORT).show()
} else {
    this.displayingPostAdapters = DisplayingPostAdapters(araayList, this)
    this.displayingPostAdapters!!.notifyDataSetChanged()
    var mlayoutManager = LinearLayoutManager(this)
    this.recylerView?.layoutManager = mlayoutManager
    this.recylerView?.itemAnimator = DefaultItemAnimator()
    this.recylerView?.adapter = displayingPostAdapters
    this.recylerView?.setHasFixedSize(true)
适配器类中的

代码

package com.internshala.test1

import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import org.json.JSONArray

class DisplayingPostAdapters(mAraay:ArrayList<Data>, context: Context) : RecyclerView.Adapter<DisplayingPostAdapters.myViewHolder>() {
    var _mArray:ArrayList<Data>? = null
    var mContext:Context? = null
    init {
        this._mArray = mAraay
        this.mContext = context
    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1) override fun onBindViewHolder(holder:myViewHolder?, position: Int) {

        Toast.makeText(mContext, "NO DATA", Toast.LENGTH_SHORT).show()

        var mDisplayingContent = _mArray?.get(position)

        holder?.mDisplay?.text = mDisplayingContent?.message
        holder?.mId?.text = mDisplayingContent?.created_time
        holder?.mCreatedTime?.text = mDisplayingContent?.id
    }

    override fun getItemCount(): Int {
        if (_mArray == null) {
            return 0
        } else {
            return (_mArray as ArrayList<Data>).size
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int):myViewHolder {
        var itemView = LayoutInflater.from(parent?.context)
            .inflate(R.layout.row_custome_data, parent, false)
        return myViewHolder(itemView)
    }

    class myViewHolder(view:View):RecyclerView.ViewHolder(view) {
        var mDisplay:TextView? = null
        var mId:TextView? = null
        var mCreatedTime:TextView? = null
        init {
            this.mDisplay = view.findViewById(R.id.display)
            this.mId = view.findViewById(R.id.mId)
            this.mCreatedTime = view.findViewById(R.id.mCreatedDate)
        }
    }
}

当我尝试运行此应用并单击“发布”按钮时,我的logcat发出此警告(logcat中发出的警告没有崩溃):

https://i.stack.imgur.com/ZI1F8.png

我的xml文件如下

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content">
        <LinearLayout
            android:layout_width="match_parent"
        android:layout_height="match_parent">
        <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=".MainActivity" >
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/recyclerView">
            </android.support.v7.widget.RecyclerView>>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

0 个答案:

没有答案
相关问题