每当活动中发生任何事件时,Listview都会复制自身

时间:2019-06-29 10:40:29

标签: android firebase-realtime-database android-listview

我有后端Firebase,用于填充列表视图,突然实现了搜索功能,突然在列表视图中发现了奇怪的行为,它在创建重复的数字,例如30-40及以上。 请帮助我,我从3天开始就开始了。我对开发非常陌生,因此对代码优化的任何建议也将作为帮助!

class Menu : AppCompatActivity(){
   private var prevKey : String? = null
    var listAdapter : CustomAdapter ? = null
    private val listItemArrayList = ArrayList<ListItem>()
    var rootRef : FirebaseDatabase? = null
    var adminRef : DatabaseReference? = null
    var mAuth : FirebaseAuth? = null
    var helper : CustomAdapter?=null
    private var user : String?=null
    var mAuthListener: FirebaseAuth.AuthStateListener? = null
        var Items = ArrayList<String>()
    var isSectionBoolean = ArrayList<Boolean>()
        var ItemCategory= ArrayList<String>()
        var ItemCost = ArrayList<Int>()
    var listView : ListView? = null

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


        mAuth = FirebaseAuth.getInstance()
        mAuthListener = FirebaseAuth.AuthStateListener {  }
        rootRef = FirebaseDatabase.getInstance()
        user =mAuth!!.currentUser!!.phoneNumber as String
        adminRef = rootRef!!.getReference("AdminCafe").child(user!!)
       listView = findViewById(R.id.admindynamiclist)
        listAdapter = CustomAdapter(listItemArrayList,this)
listView!!.adapter = listAdapter
        listView!!.isTextFilterEnabled = true



        SearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
               return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
              listAdapter!!.filter(newText.toString())

                return false
            }

        })

        getData()


    }

    fun addAct(v:View)
    {
        val intent = Intent(applicationContext,AddActivity::class.java)
        startActivity(intent)


    }



    private fun getData() {
        println("getData")




        var menucatRef = adminRef!!.child("MenuItem")
            menucatRef.addValueEventListener(object : ValueEventListener{
            override fun onCancelled(p0: DatabaseError) {

            }

            override fun onDataChange(p0: DataSnapshot) {
                for(Snap in p0.children)
                {
                    println("wtfff")


                    println("Child"+Snap.childrenCount)
                    getDataFromKey(Snap.key.toString(),Snap.childrenCount.toInt())

                }
            }

        })
    }


    private fun getDataFromKey(key: String, child :Int) {
        listItemArrayList.clear()
        println("called")

var count = 0

        var counter = 0

      var Category = CategoryHeader()

        var menuRef =  adminRef!!.child("MenuItem").child(key)
            menuRef.addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onCancelled(p0: DatabaseError) {

            }

            override fun onDataChange(p0: DataSnapshot) {
                var catKey = p0.key.toString()
            for(snap in p0!!.children)
            {


                if(count == 0)
                {


                    Category!!.setheader(catKey)
                    listItemArrayList!!.add(Category)



                    println("Seperator will be added")
                }

                else
                {
                    var hashMap = snap.value as HashMap<String,String>

                    var  itemName = snap.key
                    var isVeg = hashMap["isVeg"]
                    var itemCost = hashMap["itemCost"]!!.toInt()
                    var isSig = hashMap["isSig"]

                    var ItemHelper = Item()


                    if(itemName!=null)
                    {
                        ItemHelper.setItemName(itemName)
                    }
                    if(itemCost!=null)
                    {
                        ItemHelper.setItemCost(itemCost)
                    }
                    listItemArrayList!!.add(ItemHelper)



                }
                count++

            }


                listAdapter!!.notifyDataSetChanged()

            }

        })
    }

    interface ListItem {
        fun isHeader(): Boolean
        fun getName(): String
        fun getItemCost() : Int
    }

    fun editAct(v:View)
    {
       println("Inside edit")
    }
}

And Adapter class
class CustomAdapter(private var listItem : ArrayList<Menu.ListItem>, private  val context: Activity) : BaseAdapter() {


    var orig  =  ArrayList<Menu.ListItem> ()





    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
orig.addAll(listItem)
        println("List Item is"+listItem)
        println("List Item is"+orig)


        var holder = ViewHolder()
        var customView = convertView
   var flag = true

        if (convertView == null) {
            val layoutInflater = context.layoutInflater

            if(listItem.get(position).isHeader()) {
                println("position"+position)
                customView = layoutInflater.inflate(R.layout.seperator, null)

                holder.tvLabel = customView.categoryText




            } else {
                println("position"+position)
                customView = layoutInflater.inflate(R.layout.list_item, null)
                holder.tvLabel = customView.itemad
                holder.itemCost = customView.cost
                flag = false
  }
            customView.setTag(holder)



       }
        else
        {
            customView = convertView
            holder = convertView!!.tag as ViewHolder



        }

        if(flag) {
            println("Custoom Flag is"+flag)

            holder!!.tvLabel!!.text = listItem.get(position).getName()

        }
        else {
            println("Custoom Flag is"+flag)


            holder!!.tvLabel!!.text = listItem.get(position).getName()
            holder.itemCost!!.text = listItem.get(position).getItemCost().toString()

        }
        return customView!!
    }
    override fun getItem(position: Int): Any {
        return listItem.get(position)
    }

    override fun getItemId(position: Int): Long {
       return position.toLong()
    }

    override fun getCount(): Int {
        return listItem.size
    }

    private inner class ViewHolder {

        var tvLabel: TextView? = null
         var itemCost: TextView? = null

    }

    fun filter(query : String)
    {
        println("called")
        listItem.clear()
        var search = query

        search = search.toLowerCase(Locale.getDefault())




           if(search.isEmpty())
           {
               listItem.addAll(orig!!)
               println("Inside size 0")


           }
           else
           {
               var counter = 0
               for(result in orig!! )
               {
                   if(result.getName().toLowerCase().contains(search))
                   {
                       println("Result is "+result.getName())
                       println("listItem"+listItem)


                   }
               }
           }
  }


}

0 个答案:

没有答案