Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 回收器视图未列出,getItem count返回0_Android_Kotlin_Android Recyclerview - Fatal编程技术网

Android 回收器视图未列出,getItem count返回0

Android 回收器视图未列出,getItem count返回0,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我正在使用一个带有一些编辑文本的回收器视图。但我无法在recycler视图中列出这些值,它没有显示任何错误,但recycler视图没有显示任何内容。 我可以从API获取需要显示的数据,但在适配器类中,getItemcount()返回0大小 活动类 class Activity_addstaff : AppCompatActivity() { internal var sprefs: SharedPreferences? = null var token: String = "" private

我正在使用一个带有一些编辑文本的回收器视图。但我无法在recycler视图中列出这些值,它没有显示任何错误,但recycler视图没有显示任何内容。 我可以从API获取需要显示的数据,但在适配器类中,getItemcount()返回0大小

活动类

class Activity_addstaff : AppCompatActivity() {
internal var sprefs: SharedPreferences? = null
var token: String = ""
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<*>? = null
private var Outnameslist: MutableList<Outletnamelist>? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_addstaff)
    try {
        sprefs = this!!.getSharedPreferences(
                Config.PACKAGE_NAME, Context.MODE_PRIVATE)
        token = sprefs!!.getString("token", "No")!!
    } catch (e: Exception) {

    }
    try {


        layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        outletnameslistrecycle!!.layoutManager = layoutManager
        Outnameslist = ArrayList()
        adapter = outletname_adapter(Outnameslist as ArrayList<Outletnamelist>, this)
        outletnameslistrecycle!!.adapter = adapter
        getoutletlist();
    } catch (e: Exception) {

        e.printStackTrace()
    }

}

private fun getoutletlist() {

   Outnameslist!!.clear()

    val stringRequest = object : StringRequest(
            Request.Method.POST, Config.URL_OUTLET_NAME,
            Response.Listener<String> { response ->
                try {

                    val obj = JSONObject(response)

                    if (!obj.getBoolean("error")) {

                        val userssListArray = obj.getJSONArray("outletlist")
                        for (i in 0 until userssListArray.length()) {
                            var usersObj = userssListArray.getJSONObject(i)
                            var outlet = Outletnamelist()
                            outlet.id=usersObj.getInt("outlet_id")
                            outlet.name=usersObj.getString("outlet_name")
                            System.out.println("valuefrom api"+outlet.name)

                        }

                        adapter!!.notifyDataSetChanged()



                    } else {

                        Toast.makeText(this, obj.getString("message"), Toast.LENGTH_LONG).show()

                        if (obj.getString("message") == "Your login expired please re login") {
                            val intent = Intent(this, LoginActivity::class.java)
                            startActivity(intent)
                        }

                    }


                } catch (e: JSONException) {

                    e.printStackTrace()
                }
            },
            Response.ErrorListener {

                Toast.makeText(this, "Network Error Try Again...", Toast.LENGTH_LONG).show()

            }) {
        @Throws(AuthFailureError::class)
        override fun getParams(): Map<String, String> {
            val params = HashMap<String, String>()
            params["token"] = token

            return params
        }
    }
    stringRequest.retryPolicy = DefaultRetryPolicy(
            15000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)

    VolleySingleton.instance?.addToRequestQueue(stringRequest)

}
class outletname_adapter (internal var Outnamelist: List<Outletnamelist>, private val context: Context) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.lay_outletnamelist, parent, false)

    return ViewHolder(v, context, Outnamelist)



    return null!!
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    try {
        System.out.println("testgjo "+Outnamelist[position])
        var OutletList =Outnamelist[position]

        holder.outlet_name!!.text=OutletList.name


    } catch (e: Exception) {
        e.printStackTrace()
    }

}

override fun getItemCount(): Int {

    return Outnamelist.size

}

inner class ViewHolder(v: View, var context: Context, private val outnamelist: List<Outletnamelist>) : RecyclerView.ViewHolder(v), View.OnClickListener {

var outlet_checkbox: CheckBox? = null
   var outlet_name: TextView?=null

    var layTop: LinearLayout?= null
    init {
        itemView.setOnClickListener(this)
        try {
           outlet_checkbox=v.findViewById<View>(R.id.checkBox) as CheckBox
            outlet_name=v.findViewById<View>(R.id.txt_in)as TextView

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View) {
        val position = adapterPosition
    }
   }


   }
类活动\u addstaff:AppCompatActivity(){
内部变量sprefs:SharedReferences?=null
变量标记:String=“”
私有变量layoutManager:RecyclerView.layoutManager?=null
私有变量适配器:RecyclerView.adapter?=null
私有变量Outnameslist:MutableList?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u addstaff)
试一试{
sprefs=this!!.getSharedReferences(
Config.PACKAGE_NAME,Context.MODE_PRIVATE)
token=sprefs!!.getString(“token”,“No”)!!
}捕获(e:例外){
}
试一试{
layoutManager=LinearLayoutManager(此,LinearLayoutManager.VERTICAL,false)
outletnameslistrecycle!!.layoutManager=layoutManager
Outnameslist=ArrayList()
adapter=outletname\u adapter(Outnameslist作为ArrayList,this)
OutletNameListRecycle!!.adapter=适配器
getoutletlist();
}捕获(e:例外){
e、 printStackTrace()
}
}
私人娱乐getoutletlist(){
Outnameslist!!.clear()
val stringRequest=对象:stringRequest(
Request.Method.POST,Config.URL\u OUTLET\u NAME,
Response.Listener{Response->
试一试{
val obj=JSONObject(响应)
如果(!obj.getBoolean(“错误”)){
val usersslistary=obj.getJSONArray(“outletlist”)
for(在userssListArray.length()之前,0中的i){
var usersObj=usersslistary.getJSONObject(i)
var outlet=Outletnamelist()
outlet.id=usersObj.getInt(“outlet\u id”)
outlet.name=usersObj.getString(“outlet\u name”)
System.out.println(“valuefrom api”+outlet.name)
}
适配器!!.notifyDataSetChanged()
}否则{
Toast.makeText(this,obj.getString(“message”),Toast.LENGTH\u LONG.show()
如果(obj.getString(“消息”)=“您的登录已过期,请重新登录”){
val intent=intent(这是LoginActivity::class.java)
星触觉(意图)
}
}
}捕获(e:JSONException){
e、 printStackTrace()
}
},
Response.ErrorListener{
Toast.makeText(此“网络错误重试…”,Toast.LENGTH\u LONG.show()
}) {
@抛出(AuthFailureError::类)
重写fun getParams():映射{
val params=HashMap()
参数[“令牌”]=令牌
返回参数
}
}
stringRequest.retryPolicy=DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT\u BACKOFF\u MULT)
VolleySingleton.instance?.addToRequestQueue(stringRequest)
}
}

适配器类

class Activity_addstaff : AppCompatActivity() {
internal var sprefs: SharedPreferences? = null
var token: String = ""
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<*>? = null
private var Outnameslist: MutableList<Outletnamelist>? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_addstaff)
    try {
        sprefs = this!!.getSharedPreferences(
                Config.PACKAGE_NAME, Context.MODE_PRIVATE)
        token = sprefs!!.getString("token", "No")!!
    } catch (e: Exception) {

    }
    try {


        layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        outletnameslistrecycle!!.layoutManager = layoutManager
        Outnameslist = ArrayList()
        adapter = outletname_adapter(Outnameslist as ArrayList<Outletnamelist>, this)
        outletnameslistrecycle!!.adapter = adapter
        getoutletlist();
    } catch (e: Exception) {

        e.printStackTrace()
    }

}

private fun getoutletlist() {

   Outnameslist!!.clear()

    val stringRequest = object : StringRequest(
            Request.Method.POST, Config.URL_OUTLET_NAME,
            Response.Listener<String> { response ->
                try {

                    val obj = JSONObject(response)

                    if (!obj.getBoolean("error")) {

                        val userssListArray = obj.getJSONArray("outletlist")
                        for (i in 0 until userssListArray.length()) {
                            var usersObj = userssListArray.getJSONObject(i)
                            var outlet = Outletnamelist()
                            outlet.id=usersObj.getInt("outlet_id")
                            outlet.name=usersObj.getString("outlet_name")
                            System.out.println("valuefrom api"+outlet.name)

                        }

                        adapter!!.notifyDataSetChanged()



                    } else {

                        Toast.makeText(this, obj.getString("message"), Toast.LENGTH_LONG).show()

                        if (obj.getString("message") == "Your login expired please re login") {
                            val intent = Intent(this, LoginActivity::class.java)
                            startActivity(intent)
                        }

                    }


                } catch (e: JSONException) {

                    e.printStackTrace()
                }
            },
            Response.ErrorListener {

                Toast.makeText(this, "Network Error Try Again...", Toast.LENGTH_LONG).show()

            }) {
        @Throws(AuthFailureError::class)
        override fun getParams(): Map<String, String> {
            val params = HashMap<String, String>()
            params["token"] = token

            return params
        }
    }
    stringRequest.retryPolicy = DefaultRetryPolicy(
            15000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)

    VolleySingleton.instance?.addToRequestQueue(stringRequest)

}
class outletname_adapter (internal var Outnamelist: List<Outletnamelist>, private val context: Context) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.lay_outletnamelist, parent, false)

    return ViewHolder(v, context, Outnamelist)



    return null!!
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    try {
        System.out.println("testgjo "+Outnamelist[position])
        var OutletList =Outnamelist[position]

        holder.outlet_name!!.text=OutletList.name


    } catch (e: Exception) {
        e.printStackTrace()
    }

}

override fun getItemCount(): Int {

    return Outnamelist.size

}

inner class ViewHolder(v: View, var context: Context, private val outnamelist: List<Outletnamelist>) : RecyclerView.ViewHolder(v), View.OnClickListener {

var outlet_checkbox: CheckBox? = null
   var outlet_name: TextView?=null

    var layTop: LinearLayout?= null
    init {
        itemView.setOnClickListener(this)
        try {
           outlet_checkbox=v.findViewById<View>(R.id.checkBox) as CheckBox
            outlet_name=v.findViewById<View>(R.id.txt_in)as TextView

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onClick(v: View) {
        val position = adapterPosition
    }
   }


   }
class outletname_适配器(内部变量Outnamelist:List,私有val上下文:context):RecyclerView.adapter(){
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val v=LayoutInflater.from(parent.context)
.充气(R.layout.layout\u outletnamelist,父级,假)
返回ViewHolder(v、上下文、Outnamelist)
返回空值!!
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
试一试{
System.out.println(“testgjo”+Outnamelist[位置])
var OutletList=Outnamelist[位置]
holder.outlet\u name!!.text=OutletList.name
}捕获(e:例外){
e、 printStackTrace()
}
}
重写getItemCount():Int{
返回Outnamelist.size
}
内部类ViewHolder(v:View,var-context:context,private-val-outnamelist:List):RecyclerView.ViewHolder(v),View.OnClickListener{
var outlet_复选框:复选框?=null
变量出口名称:TextView?=null
var layTop:LinearLayout?=null
初始化{
itemView.setOnClickListener(此)
试一试{
outlet_checkbox=v.findviewbyd(R.id.checkbox)作为复选框
outlet_name=v.findviewbyd(R.id.txt_in)作为文本视图
}捕获(e:例外){
e、 printStackTrace()
}
}
覆盖趣味onClick(v:视图){
val位置=适配器位置
}
}
}
XML

<ScrollView 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"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/diagonal_bg"
    tools:context=".Activity_addstaff">

    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/img_staff"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="137dp"
        android:src="@drawable/subadmin"
        app:civ_border_color="#ffffff"
        app:civ_border_width="2dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:layout_marginTop="250dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:animateLayoutChanges="true">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffname_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffname_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_staffname" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffaddress_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffaddress_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_address" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffplace_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffplace_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_place" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffpin_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffpin_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_pin" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:animateLayoutChanges="true">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/staffmob_text_input"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/staffmob_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:hint="@string/label_mob"
                        />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout

                android:id="@+id/lay_root"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dp">


                <LinearLayout

                    android:id="@+id/lay_root2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/outletnameslistrecycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="1dp" />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:padding="20dp">

                <Button
                    android:id="@+id/btneditstaff"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:layout_gravity="center"
                    android:layout_marginEnd="5dp"
                    style="@style/DefaultButton"
                    android:background="@drawable/btn_border"
                    android:foreground="?attr/selectableItemBackground"

                    android:text="Save"
                    android:textAllCaps="false"
                    android:textColor="@color/white" />

            </LinearLayout>



        </LinearLayout>


    </LinearLayout>





</RelativeLayout>


请帮助我解决此问题。

您似乎已经创建了outlet,但没有添加到Outnameslist

解决方案-门店添加到门店列表

var outlet = Outletnamelist()
Outnameslist.add(outlet)

看起来,您已经创建了outlet,但没有添加到Outnameslist

解决方案-门店添加到门店列表

var outlet = Outletnamelist()
Outnameslist.add(outlet)