Share Function Not Working But Same Code Between Apps - kotlin

Ok, so I am trying to figure out what I am doing wrong here and I'm at a loss. I created one app with a share function to be able to email the data that is put in for later use. That code works fine and is below:
val shareButton1 = findViewById<Button>(R.id.shareButton)
shareButton1.setOnClickListener {
val contractNumber = findViewById<EditText>(R.id.contractNumber)
val conNumber = findViewById<TextView>(R.id.contractNum)
val desNumber = findViewById<EditText>(R.id.desNumber)
val desNum = findViewById<TextView>(R.id.desNum)
val lotNum = findViewById<EditText>(R.id.lotNumber)
val sublotNum = findViewById<EditText>(R.id.sublotNumber)
val genNum = findViewById<TextView>(R.id.genNum)
val ranTonnage = findViewById<TextView>(R.id.ranTonnage)
val sublotTonnage = findViewById<EditText>(R.id.sublotTonnage)
val sampleTonnage = findViewById<TextView>(R.id.sampleTonnage)
val conNumber1 = conNumber.text.toString()
val contractNumber1 = contractNumber.text.toString()
val desNum1 = desNum.text.toString()
val desNumber1 = desNumber.text.toString()
val lotNum1 = lotNum.text.toString()
val sublotNum1 = sublotNum.text.toString()
val genNum1 = genNum.text.toString()
val ranTonnage1 = ranTonnage.text.toString()
val sublotTonnage1 = sublotTonnage.text.toString()
val sampleTonnage1 = sampleTonnage.text.toString()
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TEXT, "$conNumber1 $contractNumber1 \n$desNum1 $desNumber1 \nLot #: $lotNum1 \nSublot #: $sublotNum1" +
"\nRandom Number Generated: $genNum1 \nRandom Tonnage: $ranTonnage1 \nSublot: $sublotTonnage1" +
"\nSample Tonnage: $sampleTonnage1 ")
startActivity(Intent.createChooser(shareIntent, "Share via"))
}
Now, I've created a second app to do the same thing with some different data and it isn't wanting to work. I've reviewed the code in the share function and everything seems the exact same. That code is posted below:
val shareButton1 = findViewById<Button>(R.id.share_button)
shareButton1.setOnClickListener {
val conNumber = findViewById<TextView>(R.id.contractNum)
val conNumber1 = conNumber.text.toString()
val contractNumber = findViewById<EditText>(R.id.contractNumInput)
val contractNumber1 = contractNumber.text.toString()
val desNumber = findViewById<TextView>(R.id.desNum)
val desNumber1 = desNumber.text.toString()
val desNum = findViewById<EditText>(R.id.desNumInput)
val desNum1 = desNum.text.toString()
val truckNumber = findViewById<TextView>(R.id.truckNum)
val truckNumber1 = truckNumber.text.toString()
val truckNum = findViewById<EditText>(R.id.truckNumInput)
val truckNum1 = truckNum.text.toString()
val cemDeliveredText = findViewById<TextView>(R.id.cementType1)
val cemDeliveredCalc = findViewById<TextView>(R.id.cementType1Calculated)
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_INTENT, "$conNumber1 $contractNumber1 \n$desNumber1 $desNum1 \nTruck #: $truckNum1" +
"\nCement/Type1: $cemDeliveredCalc")
startActivity(Intent.createChooser(shareIntent, "Share via"))
}
I know there has to be something simple I am missing. Like I said, the first app code works exactly as expected and pulls all the information. The second app code doesn't pull anything at all. Any help on this would be greatly appreciated as I just seem to be running in circles.

the issue with your code is in second app you can see you send
shareIntent.putExtra(Intent.EXTRA_INTENT, "$conNumber1 $contractNumber1 \n$desNumber1 $desNum1 \nTruck #: $truckNum1" +
"\nCement/Type1: $cemDeliveredCalc")
where cemDeliveredCalc is an object that's why it's not working.
you forgotted below line in your code. Add this line
val cemDeliveredCalc1 = cemDeliveredCalc.text.toString()
and then pass cemDeliveredCalc1 instead of cemDeliveredCalc
shareIntent.putExtra(Intent.EXTRA_TEXT, "$conNumber1 $contractNumber1 \n$desNumber1 $desNum1 \nTruck #: $truckNum1" +
"\nCement/Type1: $cemDeliveredCalc1")

Related

How to use a Dataframe, which is created from Dstream, outside of foreachRDD block?

i've been tried to working on spark streaming. My problem is I want to use wordCountsDataFrame again outside of the foreach block.
i want to conditionally join wordCountsDataFrame and another dataframe that is created from Dstream. Is there any way to do that or another approach?
Thanks.
My scala code block is below.
val Seq(projectId, subscription) = args.toSeq
val sparkConf = new SparkConf().setAppName("PubsubWordCount")
val ssc = new StreamingContext(sparkConf, Milliseconds(5000))
val credentail = SparkGCPCredentials.builder.build()
val pubsubStream: ReceiverInputDStream[SparkPubsubMessage] = PubsubUtils.createStream(ssc, projectId, None, subscription, credentail, StorageLevel.MEMORY_AND_DISK_SER_2)
val stream1= pubsubStream.map(message => new String(message.getData()))
stream1.foreachRDD{ rdd =>
val spark = SparkSession.builder.config(rdd.sparkContext.getConf).getOrCreate()
import spark.implicits._
// Convert RDD[String] to DataFrame
val wordsDataFrame = rdd.toDF("word")
wordsDataFrame.createOrReplaceTempView("words")
val wordCountsDataFrame =
spark.sql("select word, count(*) from words group by word")
wordCountsDataFrame.show()
}

How to use Lucene's DistinctValuesCollector?

My objective is to collect distinct values of select fields to provided them as filter options for the frontend. DistinctValuesCollector seems to be the tool for this, however since I haven't found code sample and documentation except for the Javadocs I can't currently correctly construct this collector. Can anyone provide an example?
This is my attempt which doesn't deliver the desired distinct values of the field PROJEKTSTATUS.name.
val groupSelector = TermGroupSelector(PROJEKTSTATUS.name)
val searchGroup = SearchGroup<BytesRef>()
val valueSelector = TermGroupSelector(PROJEKTSTATUS.name)
val groups = mutableListOf(searchGroup)
val distinctValuesCollector = DistinctValuesCollector(groupSelector, groups, valueSelector)
That field is indexed as follows:
document.add(TextField(PROJEKTSTATUS.name, aggregat.projektstatus, YES))
document.add(SortedDocValuesField(PROJEKTSTATUS.name, BytesRef(aggregat.projektstatus)))
Thanks to #andrewJames's hint to a test class I could figure it out:
fun IndexSearcher.collectFilterOptions(query: Query, field: String, topNGroups: Int = 128, mapper: Function<String?, String?> = Function { it }): Set<String?> {
val firstPassGroupingCollector = FirstPassGroupingCollector(TermGroupSelector(field), Sort(), topNGroups)
search(query, firstPassGroupingCollector)
val topGroups = firstPassGroupingCollector.getTopGroups(0)
val groupSelector = firstPassGroupingCollector.groupSelector
val distinctValuesCollector = DistinctValuesCollector(groupSelector, topGroups, groupSelector)
search(query, distinctValuesCollector)
return distinctValuesCollector.groups.map { mapper.apply(it.groupValue.utf8ToString()) }.toSet()
}

Why RTFEditorKit parser can not find new line?

I have a code which used javax.swing.text.rtf.RTFEditorKit
#Throws(IOException::class, BadLocationException::class)
fun readTextBlocks(`is`: InputStream?): List<TextBlock> {
val rtfParser = RTFEditorKit()
val document = rtfParser.createDefaultDocument()
rtfParser.read(`is`, document, 0)
var text = document.getText(0, document.length)
text = rtfDecoderText.decodeText(null, text)
val rootElement = document.defaultRootElement
val count = rootElement.elementCount
val blocks: MutableList<TextBlock> = ArrayList(count)
for (i in 0 until count) {
val block = TextBlock()
val childElement = rootElement.getElement(i)
if (childElement is BranchElement) {
val endOffset = childElement.getEndOffset()
if (endOffset >= text.length) {
continue
}
val substring = text.substring(childElement.getStartOffset(), endOffset)
val matchEntire = "(.*)\n".toRegex().matchEntire(substring)
and rtf text
Which produces following line (val substring )
Раздел VIIОценка соответствия объектов защиты (продукции) требованиям пожарной безопасности
I know for sure that this RTF document uses non standard spaces xA0 instead of x20 . Probably there is something wrong with new line codes and javax.swing.text.rtf.RTFEditorKit can't find them. I can see
VII\line
in the raw text. It should by "\par" instead as I understand. Could you help me to set up javax.swing.text.rtf.RTFEditorKit to parse such newlines?

Kotlin: split string from end

I want to split I_have_a_string into I_have_a and string. Is there a built in function to split from end in Kotlin? The following is what I am doing now
val words = myString.split("_")
val first = words.dropLast(1).joinToString("_")
val second = words.last()
Look at this:
val myString = "I_have_a_string"
val first = myString.substringBeforeLast("_")
val second = myString.substringAfterLast("_")
I think its self explanatory

Fixing Some Kotlin Syntax

I just finish built a simple android music app with Java, then I convert the java files to Kotlin with Kotlin plugin for Android Studio.
There are some error, in MainActivity.kt
private fun display() {
val mySongs = findSong(Environment.getExternalStorageDirectory())
items = arrayOf(mySongs.size.toString())
for (i in mySongs.indices) {
items[i] = mySongs[i].name.toString().replace(".mp3", "").replace(".wav", "")
}
val adp = ArrayAdapter(this, android.R.layout.simple_list_item_1, items!!)
listView!!.adapter = adp
listView!!.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, l -> startActivity(Intent(applicationContext, PlayerActivity::class.java).putExtra("pos", position).putExtra("songs", mySongs)) }
}
this line : items[i] = mySongs[i].name.toString().replace(".mp3","").replace(".wav", "")
showing an error: Smart cast to 'Array' is a mutable property that could have been changed by this time.
and on the PlayerActivity.kt
val i = intent
val b = i.extras
mySongs = b.getParcelableArrayList<Parcelable>(mySongs.toString())
position = b.getInt("pos", 0)
val u = Uri.parse(mySongs!![position].toString())
mediaPlayer = MediaPlayer.create(applicationContext, u)
the b.getParcelableArrayList<Parcelable>(mySongs.toString()) has a problem says Type mismatch.
Anyone can help me fix this? Thank You
This line
items = arrayOf(mySongs.size.toString())
creates an array of 1 element containing a string with the size of my songs. ex: ["23"]
You could use this instead: arrayOfNulls(mySongs.size)
For the other question:
mySongs = b.getParcelableArrayList<Parcelable>(mySongs.toString())
should return the same type as (I assume that it was the same code than in main activity because they are in 2 differents files and the context of mySongs is not provided)
val mySongs = findSong(Environment.getExternalStorageDirectory())
mySongs will have be the same type as the result of findSong.
Also you should use var instead of val because val are immutable