如何把这个arraylist在这种方法?

时间:2019-06-10 01:36:57

标签: kotlin

如何将此ArrayList放入此方法? 有人可以告诉我如何通过此方法传递此ArrayList吗? 我添加了所有片段代码,有人可以指导我吗? 我尝试了几种方法来传递此ArrayList,但始终没有成功,片段总是一片空白 谢谢

数组列表

<string-array name="object_types">

方法

private var issues: ArrayList<Issue> = ArrayList()

  private fun getIssues() {
       val stream = resources.openRawResource(R.raw.markers)
        val inputString = stream.bufferedReader().use {
            it.readText()
        }
        val klaxon = Klaxon()
        JsonReader(StringReader(inputString)).use { reader -> // requires kotlin > 1.2 !!
         reader.beginArray {
              while (reader.hasNext()) { // requires "[]" array and not {"something": []}
                    val issue = klaxon.parse<Issue>(reader)
                   this.issues!!.add(issue!!)
               }          }
        }
        this.issues = Utils.markers
    }

GalleryFragment
所有片段代码如下

class GalleryFragment : Fragment(), IssueItemOnClickListener {

    private var productsRecyclerView: RecyclerView? = null
    private var recyclerAdapter: IssueRecyclerViewAdapter? = null
    private var recyclerLayoutManager: RecyclerView.LayoutManager? = null
    private var issues: ArrayList<Issue> = ArrayList()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_gallery, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        getIssues()

        this.productsRecyclerView = view.findViewById(R.id.issue_list_recycler_view) as RecyclerView
        this.productsRecyclerView!!.setHasFixedSize(true)

        // use a grid layout manager -> 2 columns
        this.recyclerLayoutManager = GridLayoutManager(context, 2) as RecyclerView.LayoutManager?
        this.productsRecyclerView!!.layoutManager = this.recyclerLayoutManager

        this.recyclerAdapter = IssueRecyclerViewAdapter(this.issues, this)
        this.productsRecyclerView!!.adapter = recyclerAdapter
    }
    override fun onIssueItemClick(pos: Int, issue: Issue) {
        val args = Bundle()
        val detailsFragment = IssueDetailsFragment()
        args.putDouble("lat", issue.lat)
        args.putDouble("lng", issue.lng)
        args.putString("description", issue.description)
        args.putString("name", issue.name)
        args.putString("type", issue.type)
        args.putString("imgUrl", issue.imgUrl)
        detailsFragment.arguments = args

        val ft = activity!!.supportFragmentManager.beginTransaction()
        ft.replace(R.id.main_fragment_content, detailsFragment)
                .setTransition(android.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .addToBackStack(null).commit()
    }
    private fun getIssues() {
        val stream = resources.openRawResource(R.raw.markers)
        val inputString = stream.bufferedReader().use {
            it.readText()
       }
       val klaxon = Klaxon()
       JsonReader(StringReader(inputString)).use { reader -> // requires kotlin > 1.2 !!
            reader.beginArray {
                while (reader.hasNext()) { // requires "[]" array and not {"something": []}
                   val issue = klaxon.parse<Issue>(reader)
                    this.issues!!.add(issue!!)
                }
            }
        }
        this.issues = Utils.markers
    }
}

1 个答案:

答案 0 :(得分:0)

[已解决]

更改

private var issues: ArrayList<Issue> = ArrayList()

TO

private var object_values: ArrayList<Issue> = ArrayList()