Reputation: 27
I am trying to display two values that are obtained from a SQL query and present the values inside the TextViews.
barcode_field_value3.text = BarcodeProcessor.data.value1
barcode_field_value2?.text = BarcodeProcessor.data.value2
I know value1 & value2 have the correct values because I print them to the console and they are correct each time.
In my LiveBarcodeScanningActivity I tried putting the above statements inside the onCreate() but it crashes right when the app launches the activity.
I have another section below where I tried to change the text values:
workflowModel?.detectedBarcode?.observe(this, Observer { barcode ->
if (barcode != null) {
val barcodeFieldList = ArrayList<BarcodeField>()
barcodeFieldList.add(BarcodeField("Module Serial #", barcode.rawValue ?: ""))
BarcodeResultFragment.show(supportFragmentManager, barcodeFieldList)
//This prints the values to the console as a test
println("This is the RESULTS ---- " + BarcodeProcessor.data.value1)
println("This is the RESULTS ---- " + BarcodeProcessor.data.value2)
}
barcode_field_value3.text = BarcodeProcessor.data.value1
barcode_field_value2?.text = BarcodeProcessor.data.value2
})
I tried making the values nullable but nothing ever changes when they are.
/** Displays the bottom sheet to present barcode fields contained in the detected barcode. */
/**
* Controls the bottom slide in (barcode_field.xml)
*/
class BarcodeResultFragment : BottomSheetDialogFragment() {
override fun onCreateView(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup?,
bundle: Bundle?
): View {
val view = layoutInflater.inflate(R.layout.barcode_bottom_sheet, viewGroup)
val arguments = arguments
val barcodeFieldList: ArrayList<BarcodeField> =
if (arguments?.containsKey(ARG_BARCODE_FIELD_LIST) == true) {
arguments.getParcelableArrayList(ARG_BARCODE_FIELD_LIST) ?: ArrayList()
} else {
Log.e(TAG, "No barcode field list passed in!")
ArrayList()
}
view.findViewById<RecyclerView>(R.id.barcode_field_recycler_view).apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(activity)
adapter = BarcodeFieldAdapter(barcodeFieldList)
}
return view
}
override fun onDismiss(dialogInterface: DialogInterface) {
activity?.let {
// Back to working state after the bottom sheet is dismissed.
ViewModelProviders.of(it).get<WorkflowModel>(WorkflowModel::class.java)
.setWorkflowState(WorkflowState.DETECTING)
}
super.onDismiss(dialogInterface)
}
companion object {
private const val TAG = "BarcodeResultFragment"
private const val ARG_BARCODE_FIELD_LIST = "arg_barcode_field_list"
fun show(fragmentManager: FragmentManager, barcodeFieldArrayList: ArrayList<BarcodeField>) {
val barcodeResultFragment = BarcodeResultFragment()
barcodeResultFragment.arguments = Bundle().apply {
putParcelableArrayList(ARG_BARCODE_FIELD_LIST, barcodeFieldArrayList)
}
barcodeResultFragment.show(fragmentManager, TAG)
}
fun dismiss(fragmentManager: FragmentManager) {
(fragmentManager.findFragmentByTag(TAG) as BarcodeResultFragment?)?.dismiss()
}
}
}
Upvotes: 1
Views: 2734
Reputation: 91
Please try this
val requestCode=100
startActivityForResult(Intent(context,MainActivity::class.java),requestCode)
and In barcode scanner activity
val data = Intent();
val text = "Result to be returned...."
//---set the data to pass back---
data.setData("Your Data");
setResult(RESULT_OK, data);
//---close the activity---
finish()
and get in main activity like this
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent):Void {
if (requestCode == requestCode) {
if (resultCode == RESULT_OK) {
}
}
}
Upvotes: 1
Reputation: 91
In fragment you have to use widget like this
view.barcode_field_value3.text=
view.barcode_field_value2.text=
Here view is the main layout and declare it in onCreateView like this
view = inflater.inflate(R.layout.fragment_dashboard, container, false)
Upvotes: 1
Reputation: 91
The error says "barcode_field_value3 must not be null". Verify the code is running only after you've initialized the barcode_field_value3 member
Upvotes: 1