我想从非父文件访问和更改文本视图的文本。
这是我想从中进行编辑的类
class BarcodeProcessor(graphicOverlay: GraphicOverlay, private val workflowModel: WorkflowModel) :
FrameProcessorBase<List<FirebaseVisionBarcode>>() {
在这个类中,没有onCreat((
我是否需要具有 AppCompatActivity(( 才能访问 xml 布局文件?
在此文件中是处理条形码的位置,我可以向控制台显示结果以及SQL查询,但我想更新界面以反映这些值。我可以通过伴随对象将它们传递给父类吗?
有什么方法可以从条形码处理器文件进行布局更新吗?
我试过包括导入
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(( 中。不过,我包含了整个文件以供参考。
实时条形码扫描活动
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)
}
})
}```
您正在BarcodeProcessor
中设置workflowModel.detectedBarcode
实时数据。因此,您可以找到将此workflowModel
传递给构造函数的位置BarcodeProcessor
并添加这样的代码以在活动中观察此实时数据:
workflowModel.detectedBarcode.observe(this, Observer {
myTextView.text = it
})
此答案直接涉及在 Google ML-Kit Showcase 示例项目中显示结果。因此,如果您使用条形码扫描进行研究,这是相关的。
为了在成功扫描时显示结果,您需要将字段添加到
条形码字段适配器文件
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
}
里面
条码处理器
声明数据对象
object data {
var finalDisposition = "init"
var moduleSize = "init"
var barcodeValue: String = ""
var NCRNotes = ""
var OverlapLength = ""
}
这些变量将被传递到上面的 BarcodeFieldAdapter 文件中,然后在成功时显示在barcode_field.xml中。
在查询函数内 - 即在条形码处理器文件中
//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( 创建 TextViews,如上面的 BarcodeFieldAdapter 文件所示。现在,成功扫描后,将显示查询中的新值。