通过光标填充无线电组

时间:2019-11-12 06:23:20

标签: android kotlin radio-button radio-group

程序从Google.com中的用户个人资料中请求日历列表。

    val root = inflater.inflate(R.layout.fragment_tab_settings, container, false)
    val projection = arrayOf(
        CalendarContract.Calendars._ID, // 0
        CalendarContract.Calendars.ACCOUNT_NAME, // 1
        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
        CalendarContract.Calendars.OWNER_ACCOUNT // 3
    )
    val uri: Uri = CalendarContract.Calendars.CONTENT_URI
    var cur: Cursor? = null
    val cr = activity!!.contentResolver
    val selection =
        ("((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND (" + 
          CalendarContract.Calendars.ACCOUNT_TYPE + " = ?))")
    val selectionArgs = arrayOf("account_name@gmail.com", "com.google")
    cur = cr.query(uri, projection, selection, selectionArgs, null)

此外,此列表放置在radioGroup中供选择。 通过一会儿实现了这一点。

    val linearLayout = root.findViewById<LinearLayout>(R.id.rootLayout)
    val radioGroup = root.findViewById<RadioGroup>(R.id.radioGroup)
    if (cur != null) {
        if (cur.count > 0) {
            cur.moveToFirst()
            do {
                val radioButton1 = RadioButton(context)
                radioButton1.layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                radioButton1.text = cur.getString(2)
                radioButton1.id = cur.getInt(0)
                radioGroup.addView(radioButton1)
            } while (cur.moveToNext())
        }
        cur.close()
    }

有没有更优雅的解决方案?

0 个答案:

没有答案