I don't know what mistake I am doing why it is returning null, I have seen other people problems I am not getting what I need exactly. I am sending the string data in this activity
val inspenctionIntent = Intent(this, InspectActivity::class.java)
inspenctionIntent.putExtra("Particulars", estimateItem.Particulars)
inspenctionIntent.putExtra("SSRItemNO", estimateItem.SSRItemNO)
inspenctionIntent.putExtra("Quantity", estimateItem.Quantity)
inspenctionIntent.putExtra("QuantityUnit", estimateItem.QuantityUnit)
inspenctionIntent.putExtra("Times", estimateItem.Times)
inspenctionIntent.putExtra("Rates", estimateItem.Rates)
inspenctionIntent.putExtra("RatesPer", estimateItem.RatesPer)
inspenctionIntent.putExtra("Total", estimateItem.Total)
startActivity(Intent(this#CheckMeasurementActivity, InspectActivity::class.java))
and trying to get that string extra in another activity but is returning null?
val Particulars = intent.getStringExtra("Particulars")
val SSRItemNO = intent.getStringExtra("SSRItemNO")
val Quantity = intent.getStringExtra("Quantity")
val QuantityUnit = intent.getStringExtra("QuantityUnit")
val Times = intent.getStringExtra("Times")
val Rates = intent.getStringExtra("Rates")
val RatesPer = intent.getStringExtra("RatesPer")
val Total = intent.getStringExtra("Total")
I tried by replacing var also still it is returning null?
Your error is this line.
startActivity(Intent(this#CheckMeasurementActivity, InspectActivity::class.java))
You declare inspenctionIntent but never use it, so others activity will get nothing since your intent send nothing to it.
So you need to change
startActivity(Intent(this#CheckMeasurementActivity, InspectActivity::class.java))
to
startActivity(inspenctionIntent)
You are started wrong intent so should use inspenctionIntent inside startActivity()
val inspenctionIntent = Intent(this, InspectActivity::class.java)
inspenctionIntent.putExtra("Particulars", estimateItem.Particulars)
inspenctionIntent.putExtra("SSRItemNO", estimateItem.SSRItemNO)
inspenctionIntent.putExtra("Quantity", estimateItem.Quantity)
inspenctionIntent.putExtra("QuantityUnit", estimateItem.QuantityUnit)
inspenctionIntent.putExtra("Times", estimateItem.Times)
inspenctionIntent.putExtra("Rates", estimateItem.Rates)
inspenctionIntent.putExtra("RatesPer", estimateItem.RatesPer)
inspenctionIntent.putExtra("Total", estimateItem.Total)
startActivity(inspenctionIntent)
You should write
Intent.putExtra("Particulars", estimateItem.Particulars)
Instead of
inspenctionIntent.putExtra("Particulars", estimateItem.Particulars)
and for others is the same.
Just change this-:
startActivity(Intent(this#CheckMeasurementActivity, InspectActivity::class.java))
to-:
startActivity(inspenctionIntent)
in last line of your code...
Related
fun SubmitOrder(view: View) {
/* pricing of coffee */
val total = quantity * 5
val s: String = ("$$total.00")
money.text = ("Total : $s\nThank You!").toString()
//This is calling On click listener
Toast.makeText(this, "order_Submitted", Toast.LENGTH_SHORT).show()
}
In this code, I need a new line before Thank You! in money.text but I am not getting any new line I am new in android development so, am not able to point out the mistake.
Let's go thru your code line by line:
val s: String = ("$$total.00")
s is a bad variable name, as it's not descriptive at all
you don't need the (braces)
the :String here is optional. In such an obvious case i would emit it.
A $ is a sign to the kotlin compiler to include the following variable. Therefore, you can't use the $ when you mean "US-Dollar". See this post on how to escape it
While ".00" works, it's no good style. I suggest you use string formatting as described here.
can be written as val s = "\$ ${String.format("%.2f", total)}"
you should wherever possible use string resources, but thats out of the scope of this answer
money.text = ("Total : $s\nThank You!").toString()
this is correct, but unnecessary verbose:
"Total : $s\nThank You!" is already a string, so there's no need to .toString()
braces are not needed
can be written as money.text = "Total : $s\nThank You!"
What i am trying to do is to convert the ArrayList<Model> to MutableLiveData<ArrayList<Model>> to be send as a return value. Though i am getting the ArrayList<Model> result correctly,i failed miserably in posting the value to MutableLiveData<ArrayList<Model>>.
This is what i am trying to do...
suspend fun getSeasonBodyWeight(): MutableLiveData<ArrayList<MSBodyWeight>> {
val x = MutableLiveData<ArrayList<MSBodyWeight>>()
val y:ArrayList<MSBodyWeight> = getBWeightCoroutine()
x.postValue(y)
Log.i(TAG, "Check ${y.size}")
Log.i(TAG, "Check ${x.value}")
return x
}
This is what i getting in Logcat
I/FirebaseConnect: Check 2
I/FirebaseConnect: Check null
So what am i doing wrong. Also how to convert ArrayList<Model> to MutableLiveData<ArrayList<Model>>
I am trying to learn Kotlin.. Please bear with me if its a NOOB question.
Thanks
When using postValue and if you check the source code you will find in the method description:
Posts a task to a main thread to set the given value.
Means that the value will not be immediately set, it starts a task to change it. If you mean to change the value immediately you should use:
x.value = y
The difference between the two is that you cannot call setValue from a background thread, that meaning, if you are in a background thread you should call postValue. If you are in the main thread setValue may be used
suspend fun getSeasonBodyWeight(): MutableLiveData<ArrayList<MSBodyWeight>> {
val x = MutableLiveData<ArrayList<MSBodyWeight>>()
x.value = arrayListOf();
val y:ArrayList<MSBodyWeight> = getBWeightCoroutine()
x.postValue(y)
Log.i(TAG, "Check ${y.size}")
Log.i(TAG, "Check ${x.value}")
return x
}
I have a user interface with multiple buttons. They have the IDs "button1", "button2", ...
I want to set an OnClickListener for all of them in a for loop. I dont want to type a line like button1.setOnClickListener for every button.
I have found one solution that works in java here: Android: Using findViewById() with a string / in a loop
And I tried to adapt it in Kotlin.
var buttons = ArrayList<Button>()
for (i in 1..7) {
var idString = "Button%i"
var buttonID = getResources().getIdentifier(idString, "id", packageName)
buttons.add( findViewWithTag(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}
This throws an "Unresolved Reference" error. How can I access all buttons without typing a line for each of them?
Thanks in advance to all of you.
You call findViewWithTag() instead of findViewById() in your code.
Also you are not doing string interpolation correctly by var idString = "Button%i".
Change to this:
val buttons = ArrayList<Button>()
for (i in 1..7) {
val idString = "Button$i"
val buttonID = resources.getIdentifier(idString, "id", packageName)
buttons.add(findViewById(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}
I am trying to concatenate 2 String but not sure how to go about it.
this is my code:
val word = R.string.word
and i'm trying to append it with "$currentPage/5" inside the setText("$currentPage/5")
i tried to make it in this way setText("$word $currentPage/5")
and this way setText("${R.string.value} $currentPage/5")
and it did not work , it only shows me numbers not the text
try to use this:
val word = getString(R.string.word)
text_view.text = "$word $currentPage/5"
If you want to edit your value (e.g. current page) wrap it with {}
E.g.
val word = getString(R.string.word)
text_view.text = "$word ${currentPage/5}"
Remember to use proper kotlin syntax
In Kotlin, the concatenation of string can be done by **interpolation/templates**.
val a = "Its"
val b = "Kotlin!"
val c = "$a $b"
The output will be Its Kotlin!
Or we can alson do concatenate using the **+ / plus() operator**:
val a = "String"
val b = "Concatenate"
val c = a + b
val d =a.plus(b)
print(c)
The output will be: StringConcatenate
print(d)
The output will be: StringConcatenate
Or you can concatenate using the StringBuilder which is a normal way to do that.
To concatenate two string, we could do
val concatenatedWord = "${resources.getString(R.string.value)}:
${number/3}."
If R.string.value was "The result" and number was 15, value of concatenatedWord will be "The result: 5."
Or we could also concatenate using the + operator or using StringBuilder.
But if you do
textView.text = "${resources.getString(R.string.value)}: ${number/3}."
AS will warn "Do not concatenate text displayed with setText." so, in the case of setting concatenated text in textview, consider using
String.format("%s: %d.", resources.getString(R.string.value):
number/3)
As a future resource and answer why the accepted answer works:-
String Templates:-
Strings may contain template expressions, i.e. pieces of code that are evaluated and whose results are concatenated into the string.
How to implement these?
A template expression should start with a dollar sign ($) and consists of either a simple name:
when the expression is a simple variable.
val i = 10
println("i = $i") // prints "i = 10"
or else arbitrary expression in curly braces:
val s = "abc"
println("$s.length is ${s.length}") // prints "abc.length is 3"
Note :- Templates are supported both inside raw strings and inside escaped strings.
val nameOfAnimal = "fish"
val speciesClass = "is an Aquatic Vertebrate"
println(nameOfAnimal.plus(speciesClass))
println(nameOfAnimal+speciesClass)
println("$nameOfAnimal $speciesClass")
Results:
fishis an Aquatic Vertebrate
fishis an Aquatic Vertebrate
fish is an Aquatic Vertebrate
When I try to send more than one value with Intent, on target activity only last value is visible. How I can send multiple key,values with Intent?
MainActivity.java
intent.putExtra(SITE_NAME, site_name);
intent.putExtra(SITE_ADDRESS, site_adress);
AnotherActivity.java
site_name = intent.getStringExtra(MainActivity.SITE_ADDRESS);
site_address = intent.getStringExtra(MainActivity.SITE_NAME);
When I change the order of putExtras last putExtra is avaible.
You posted this:
site_name = intent.getStringExtra(MainActivity.SITE_ADDRESS);
site_address = intent.getStringExtra(MainActivity.SITE_NAME);
I believe you meant this, they're backwards:
site_name = intent.getStringExtra(MainActivity.SITE_NAME);
site_address = intent.getStringExtra(MainActivity.SITE_ADDRESS);