如何从非父文件访问TextView [Android]

时间:2019-07-16 12:52:28

标签: android android-layout kotlin

我想从非父文件访问和更改TextView的文本。

这是我要从中进行编辑的课程

class BarcodeProcessor(graphicOverlay: GraphicOverlay, private val workflowModel: WorkflowModel) :
    FrameProcessorBase<List<FirebaseVisionBarcode>>() {

在此类中,没有onCreat()

我是否需要具有AppCompatActivity()才能访问xml布局文件?

在此文件内是处理条形码的位置,我可以将结果以及SQL查询显示到控制台,但是我想更新界面以反映这些值。我可以通过陪伴对象将它们传递给父类吗?

我可以通过BarcodeProcessor文件进行布局更新吗?

我尝试过包括进口

import kotlinx.android.synthetic.main.barcode_field.*
    @MainThread
    override fun onSuccess(
        image: FirebaseVisionImage,
        results: List<FirebaseVisionBarcode>,
        graphicOverlay: GraphicOverlay
    ) {

        var z = ""
        var isSuccess: Boolean? = false
        //Variables used in the SQL query
        var strData = ""
        var strData2 = ""

        try {
            con = dbConn()      // Connect to database
            if (con == null) {
                z = "Check Your Internet Access!"
            } else {
                val query = "select [ValueOne], [ValueTwo] from [TableName] where [ValueOne] = '$barcodeValue'"
                val stmt = con!!.createStatement()
                val cursor = stmt.executeQuery(query)
                barcodeValue = results[0].getRawValue()!!
                println(barcodeValue)
                if (cursor.next()) {
                    //Selects the values from the columns in the table where the query is taking place
                    strData = cursor.getString("[ValueOne]")
                    strData2 = cursor.getString("[ValueTwo]")
                    var finalDispositionID = strData
                    var moduleSizeID = strData2
                    println(finalDispositionID)
                    println(moduleSizeID)

                    //barcode_field_value3 is the name of the textview
                    //barcode_field_value2 is the name of the other textview

                    //barcode_field_value3.text = strData

                    z = "Login successful"
                    isSuccess = true
                    con!!.close()
                } else {
                    z = "Invalid Credentials!"
                    isSuccess = false
                }
            }
        } catch (ex: java.lang.Exception) {
            isSuccess = false
            z = ex.message!!
        }

查询在函数onSuccess()中进行。我将整个文件都包括在内以供参考。

LiveBarcodeScanningActivity

    private fun setUpWorkflowModel() {
        workflowModel = ViewModelProviders.of(this).get(WorkflowModel::class.java)

        // Observes the workflow state changes, if happens, update the overlay view indicators and
        // camera preview state.
        workflowModel!!.workflowState.observe(this, Observer { workflowState ->
            if (workflowState == null || Objects.equal(currentWorkflowState, workflowState)) {
                return@Observer
            }

            currentWorkflowState = workflowState
            Log.d(TAG, "Current workflow state: ${currentWorkflowState!!.name}")

            val wasPromptChipGone = promptChip?.visibility == View.GONE

            when (workflowState) {
                WorkflowState.DETECTING -> {
                    promptChip?.visibility = View.VISIBLE
                    promptChip?.setText(R.string.prompt_point_at_a_barcode)
                    startCameraPreview()
                }
                WorkflowState.CONFIRMING -> {
                    promptChip?.visibility = View.VISIBLE
                    promptChip?.setText(R.string.prompt_move_camera_closer)
                    startCameraPreview()
                }
                WorkflowState.SEARCHING -> {
                    promptChip?.visibility = View.VISIBLE
                    promptChip?.setText(R.string.prompt_searching)
                    stopCameraPreview()
                }
                WorkflowState.DETECTED, WorkflowState.SEARCHED -> {
                    promptChip?.visibility = View.GONE
                    stopCameraPreview()
                }
                else -> promptChip?.visibility = View.GONE
            }

            val shouldPlayPromptChipEnteringAnimation = wasPromptChipGone && promptChip?.visibility == View.VISIBLE
            promptChipAnimator?.let {
                if (shouldPlayPromptChipEnteringAnimation && !it.isRunning) it.start()
            }
        })


        workflowModel?.detectedBarcode?.observe(this, Observer { barcode ->

            if (barcode != null) {
                val barcodeFieldList = ArrayList<BarcodeField>()
                barcodeFieldList.add(BarcodeField("Module Serial Number", barcode.rawValue ?: ""))
                BarcodeResultFragment.show(supportFragmentManager, barcodeFieldList)

            }
        })

    }```

2 个答案:

答案 0 :(得分:0)

您正在workflowModel.detectedBarcode中设置BarcodeProcessor个实时数据。因此,您可以找到将workflowModel传递给BarcodeProcessor构造函数的位置,并添加这样的代码以观察活动中的实时数据:

workflowModel.detectedBarcode.observe(this, Observer { 
    myTextView.text = it
})

答案 1 :(得分:0)

此答案直接与在Google ML-Kit Showcase示例项目中显示结果有关。因此,如果您使用条形码扫描进行这项工作就可以了。

为了在成功扫描时显示结果,您需要将字段添加到

BarcodeFieldAdapter文件

    internal class BarcodeFieldViewHolder private constructor(view: View) : RecyclerView.ViewHolder(view) {

        private val labelView: TextView = view.findViewById(R.id.barcode_field_label)
        private val valueView: TextView = view.findViewById(R.id.barcode_field_value)
        private val moduleSize: TextView = view.findViewById(R.id.textViewModuleSize)
        private val finalDisposition: TextView = view.findViewById(R.id.textViewFinalDisp)
        private val NCRNotes: TextView = view.findViewById(R.id.textViewNCR)
        private val OverlapLengthText: TextView = view.findViewById((R.id.textViewOverlapLength))

        fun bindBarcodeField(barcodeField: BarcodeField) {
            //These are the original values that display in the Barcode_field.xml
            labelView.text = barcodeField.label
            valueView.text = barcodeField.value

            //These are the new values I passed in
            moduleSize.text = BarcodeProcessor.data.moduleSize
            finalDisposition.text = BarcodeProcessor.data.finalDisposition
            NCRNotes.text = BarcodeProcessor.data.NCRNotes
            OverlapLengthText.text = BarcodeProcessor.data.OverlapLength
        }

内部

BarcodeProcessor

声明数据对象

    object data {
        var finalDisposition = "init"
        var moduleSize = "init"
        var barcodeValue: String = ""
        var NCRNotes = ""
        var OverlapLength = ""
    }

这些变量将被传递到上面的BarcodeFieldAdapter文件中,并在成功后显示在barcode_field.xml中。

查询功能内-位于BarcodeProcessor文件中

//Selects the values from the columns in the table where the query is taking place
                    strData = cursor.getString("Column Name 1")
                    strData2 = cursor.getString("Column Name 2")
                    strData4 = cursor.getString("Column Name 3")

                    //Assigns the values to the variables inside the object data

                    data.finalDisposition = strData
                    data.moduleSize = strData2
                    data.OverlapLength = strData4
                    data.NCRNotes = ""

最后,在Barcode_field.xml文件中,使用(R.id.TextViewNAMES)创建TextView,如上面在BarcodeFieldAdapter文件中所示。现在,成功扫描后,将显示查询中的新值。