Home > Net >  How to seperate my List<Strings> to one by one for grid view
How to seperate my List<Strings> to one by one for grid view

Time:09-29

Hi I have been trying to seperate my list of washing machines to make them one by one in grid view but for some reason they keep getting stuck together like in the image.enter image description here

My Fragment that has most of the work going on in the background.

class HomeFragment : Fragment() {

    private lateinit var binding: FragmentHomeBinding
    private val viewModel by KoinJavaComponent.inject(HomeFragmentVM::class.java)
    private val sp by inject<SPManager>()
    private var profile : Data? = null

    private val itemList: Array<String>
        get() = arrayOf(viewModel.products.toString())





    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.getWashingMachines()
        setObservers()
        setupGridView()

    }

    private fun setupGridView() {
        println(viewModel.products)

    }

    private fun setObservers() {
        val strings = arrayOf(viewModel.products)
        viewModel.isCorrect.observe(viewLifecycleOwner) {
            if (it.isNotEmpty()){
                itemList.contentEquals(arrayOf(viewModel.products))
                val adapter = ImageListAdapter(context!!, R.layout.list_item, itemList)
                binding.gridview.adapter = adapter
                binding.gridview.onItemClickListener =
                    AdapterView.OnItemClickListener { parent, v, position, id ->
                        println("you clicked")
                    }
                println("Not Failed")

            }
            else
            {
                println("Failed")
            }
        }

    }
class HomeFragment : Fragment() {

    private lateinit var binding: FragmentHomeBinding
    private val viewModel by KoinJavaComponent.inject(HomeFragmentVM::class.java)
    private val sp by inject<SPManager>()
    private var profile : Data? = null

    private val itemList: Array<String>
        get() = arrayOf(viewModel.products.toString())





    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.getWashingMachines()
        setObservers()
        setupGridView()

    }

    private fun setupGridView() {
        println(viewModel.products)

    }

    private fun setObservers() {
        val strings = arrayOf(viewModel.products)
        viewModel.isCorrect.observe(viewLifecycleOwner) {
            if (it.isNotEmpty()){
                itemList.contentEquals(arrayOf(viewModel.products))
                val adapter = ImageListAdapter(context!!, R.layout.list_item, itemList)
                binding.gridview.adapter = adapter
                binding.gridview.onItemClickListener =
                    AdapterView.OnItemClickListener { parent, v, position, id ->
                        println("you clicked")
                    }
                println("Not Failed")

            }
            else
            {
                println("Failed")
            }
        }

    }

The Adapter

internal class ImageListAdapter internal constructor(
    context: Context,
    private val resource: Int,
    private val itemList: Array<String>?
) : ArrayAdapter<ImageListAdapter.ItemViewHolder>(context, resource) {

    private val inflater: LayoutInflater = LayoutInflater.from(context)
    private lateinit var itemBinding: ListItemBinding

    override fun getCount(): Int {
        return if (this.itemList != null) this.itemList.size else 0
    }

    override fun getView(position: Int, view: View?, parent: ViewGroup): View {
        var convertView = view
        val holder: ItemViewHolder
        if (convertView == null) {
            itemBinding = ListItemBinding.inflate(inflater)
            convertView = itemBinding.root
            holder = ItemViewHolder()
            holder.name = itemBinding.textView
            holder.icon = itemBinding.icon
            convertView.tag = holder
        } else {
            holder = convertView.tag as ItemViewHolder
        }
        holder.name!!.text = this.itemList!![position]
        holder.icon!!.setImageResource(R.mipmap.ic_launcher)
        return convertView
    }

    internal class ItemViewHolder {
        var name: TextView? = null
        var icon: ImageView? = null
    }
}

The Api call works its just how do I separate this one by one to make each a grid layout view many thanks

CodePudding user response:

After sawing your code, I think the problem is on

private val itemList: Array<String>
    get() = arrayOf(viewModel.products.toString())

Because you are converting your list of products to ONE string when you call 'toString()', and inserting that one string into one list, passing than that one list with one item (the string) to the adapter

CodePudding user response:

You should replace

private val itemList: Array<String>
    get() = arrayOf(viewModel.products.toString())

with

private val itemList:Array<String>
    get() {
        val out = viewModel.products.map { it ->
            it.toString()
        }
        return out
    }
  • Related