如何使用Kotlin在回收者视图中的itemclick上删除SharedPreference

时间:2019-07-09 13:26:59

标签: android kotlin sharedpreferences

我用Kotlin创建了一个android应用。该应用的描述如下:一个recyclerview horizo​​ntal包含所有类别列表,我使用适配器。然后,当我单击此列表中的项目时,产品列表所属的列表出现在recyclerview的底部。性格  产品列表的确定通过两种方式完成:列表或网格。 以下代码是活动:

class CategoryByProduct : AppCompatActivity(), CategoryAdapter.OnItemClickListener {
    var mobileApi: MobileApi? = null


    override fun onItemClick(view: View, viewModel: ProductCategoryData) {
    Utility().removeValue(this,"Products")
        var params = HashMap<String, String>()
        params["ProductCategoryID"] = viewModel.id.toString()
        GlobalScope.launch(Dispatchers.IO) {
            val products = mobileApi!!.apiMobileProductsGetAllPost(params, 0, 50, "", "")

            Utility().setArrayDataByKeyValue(this@CategoryByProduct, "Products", products)
            withContext(Dispatchers.Main) {
                recyclerViewProductByCategory.apply {
                    recyclerViewProductByCategory.layoutManager =
                        StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                    recyclerViewProductByCategory.adapter = ProductAdapter(products)
                }

            }

        }

    }

    override fun onResume() {
        super.onResume()
        val productsSp = Utility().getArrayDataByKey(this@CategoryByProduct, "Products")

        gridProducts.setOnClickListener {
            recyclerViewProductByCategory.layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            recyclerViewProductByCategory.adapter = productsSp?.let { it ->
                ProductAdapter(
                    it
                )
            }
            gridProducts.visibility = View.INVISIBLE
            listProducts.visibility = View.VISIBLE

        }
        listProducts.setOnClickListener {
            recyclerViewProductByCategory.layoutManager =
                LinearLayoutManager(this@CategoryByProduct, RecyclerView.VERTICAL, false)
            recyclerViewProductByCategory.adapter = productsSp?.let { it -> ProductAdapterList(it) }
            listProducts.visibility = View.INVISIBLE
            gridProducts.visibility = View.VISIBLE
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.list_product_by_category)
        mobileApi = MobileApi()
        val params = HashMap<String, String>()
        GlobalScope.launch(Dispatchers.IO) {

            val categories = mobileApi!!.apiMobileProductCategoriesGetAllPost(params, 0, 50, "", "")
            val products = mobileApi!!.apiMobileProductsGetAllPost(params, 0, 50, "", "")
            withContext(Dispatchers.Main) {
                recyclerViewCategories.apply {
                    recyclerViewCategories.layoutManager =
                        LinearLayoutManager(this@CategoryByProduct, RecyclerView.HORIZONTAL, false)
                    recyclerViewCategories.adapter = CategoryAdapter(categories, this@CategoryByProduct)
                    productNumber.text = "${products.size} Products"
                }
            }

        }
    }


}

我的问题是,当我单击类别时,出现产品列表,然后,当我单击另一个类别时,出现最后一项的产品列表而不是所单击类别的产品列表。 我认为删除sharedpreference仍然无法正常工作。 以下代码用于sharedpreferences:

class Utility {


    fun setArrayDataByKeyValue(context: Context, key: String, DataArrayList: Array<ProductData>) {
        val arrayString = Gson().toJson(DataArrayList)
        val sharedPreferences = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)
        sharedPreferences.edit().putString(key, arrayString).apply()
    }


    fun getArrayDataByKey(context: Context, key: String): Array<ProductData>? {
        val sharedPreferences = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)
        val emptyList = Gson().toJson(ArrayList<ProductData>())
        return Gson().fromJson<Array<ProductData>>(sharedPreferences.getString(key, emptyList), object :
            TypeToken<Array<ProductData>>() {

        }.type)

    }

    fun removeValue(context: Context, key: String) {
        val sharedPreferences = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)
        val editor: SharedPreferences.Editor = sharedPreferences.edit()
        editor.remove(key)
        editor.apply()
    }

}

我如何使我的代码起作用。

0 个答案:

没有答案