Using string translation in adapter return numbers - kotlin

I am trying to use translation string in my adapter but it returns numbers instead
if(currentItem.budget != null){
holder.budget.text = "$ " + currentItem.budget.format()
} else {
holder.budget.text = R.string.open_to_suggestions.toString()
}
R.string.open_to_suggestions.toString() supposed to return string text Open to suggestions but it returns numbers such as 2131755113 not sure why! any idea?

To show the string resource you must use context.getString()
if(currentItem.budget != null) {
holder.budget.text = "$ " + currentItem.budget.format()
} else {
val context = holder.itemView.context
holder.budget.text = context.getString(R.string.open_to_suggestions)
}
Please take a look at the definition of getString here

Related

`string index out of range 0` how to resolve it?

The toCamelCase function takes in any kind of string and converts it into camelCase or Pascal case;
fun toCamelCase(str:String): String {
var ans: String = str[0].toString()
for(i in 1..str.length - 1) {
if(str[i] != '-' && str[i] != '_' ) {
ans += str[i]
}
}
return ans
}
But the code above produces the following error
String index out of range: 0
I am a novice so please help me understand.
Edit: This piece of code worked out without any error in Kotlin playground( online kotlin editor)
but is not working in codewars website
I think your code may provide that kind of error if you call your function with an empty string: e.g. toCamelCase(""). This is because you try to access index 0 regardless of the input string (in str[0].toString()).
To avoid that, you should build your ans from an empty string and append characters starting from index 0, for example changing your for(i in 1..str.length - 1) to for(i in 0..str.length - 1).
An example fix can be as follows:
fun toCamelCase(str:String): String {
var ans: String = ""
for(i in 0..str.length - 1) {
if(str[i] != '-' && str[i] != '_' ) {
ans += str[i]
}
}
return ans
}

XMLPullParser returns only one element

I could not parse my XML file, it returns only one element instead of 4
Here's my XML file
<Quizzs>
<Quizz type="A">...</Quizz>
<Quizz type="B">...</Quizz>
<Quizz type="C">...</Quizz>
<Quizz type="D">...</Quizz>
</Quizzs>
It returns only the last one "D"
while (eventType != XmlPullParser.END_DOCUMENT) {
var eltName: String? = null
when (eventType) {
XmlPullParser.START_TAG -> {
eltName = parser.name
if ("Quizzs" == eltName) {
currentQuizz = Quizz()
quizz.add(currentQuizz)
} else if (currentQuizz != null) {
if ("Quizz" == eltName) {
currentQuizz.type = parser.getAttributeValue(null, "type")
}
}
}
}
eventType = parser.next()
}
printPlayers(quizz)
}
You need to .add() something to your currentQuizz for each "Quizz". With currentQuizz.type = ... you just overwrite each previous "Quizz" with the current one, so you end up with only the last one, which is D.
I think you are confused by your own code. For the "Quizzs" tag you create a Quizz() object instead of a QuizzList() object or something similar. It is for the "Quizz" tag you should create a new Quizz() object each time. And then you should add that object to your QuizzList.

Reduction number of IF in my case

I have too much IF in my method like this:
if (myObject?.name !=null)
first.text = myObject.name.bigThing
if (myObject?.age !=null)
second.text = myObject.age.bigThing
if (myObject?.surname !=null)
third.text = myObject.surname.bigThing
and 20 more ...
How can I shorten the code?
age/surname/name is type my own class Big with id: Int and bigThing: String
One way could be:
myObject?.age?.let { second.text = it.bigThing }
If you're putting the value inside a TextView:
first.text = myObject?.age?.bigThing
One option would be
fun updateText(x: WhateverTheTypeOfFirstSecondEtcIs, y: Big?) {
if (y != null) { x.text = y.bigThing }
}
updateText(first, myObject?.name)
updateText(second, myObject?.age)
updateText(third, myObject?.surname)
Change each of them like this if you like it:
myObject?.name?.run { first.text = this.bigThing }
I don't know the exact syntax in kotlin, however I would expect to be able to create a method something like this:
String extractBigThing(Big big){
if(big != null) return big. GetBigThing() ;
return null;
}
And call it something like this :
first.text = extractBigThing(myObject.name);
The basic idea is to extract reused functionality into reusable code, these can then be unit tested to increase code robustness.
I hope this helps.
myObject?.name?.bigThing?.let { first.text = it } etc for each line
or
myObject?.apply {
name?.bigThing?.let { first.text = it }
age?.bigThing?.let { second.text = it }
}
etc

Visual SourceSafe script starting out

I have never wrote a script before and I was asked today to make a Visual SourceSafe script that returns all of the labels that are stored.
I have 0 idea on how to start this as I have never wrote a script before. Can anybody point me in the right direction with this please?
Thanks!
You can use the History command of SourceSafe to get the history info of an item and extract the label info you need.
Here is a simple sample for you:
private void GetItem(VSSItem vssItem)
{
if (vssItem.Type == 0) //Type == 0 means it's a project
{
bool bIncludeDeleted = false;
IVSSItems vssItems = vssItem.get_Items(bIncludeDeleted);
foreach (VSSItem vssitem in vssItems)
{
GetItem(vssitem);
foreach (IVSSVersion vssVersion in vssitem.get_Versions(0))
{
string vssItemName = "";
if (vssVersion.VSSItem.Name == "")
vssItemName = vssitem.Spec;
else
vssItemName = vssVersion.VSSItem.Spec;
if (vssVersion.Action.IndexOf("Label") > -1 )
{
if (vssitem.Spec == vssVersion.VSSItem.Spec)
{
MessageBox.Show("Item " + vssItemName + " in " + "Version " + vssVersion.VersionNumber.ToString() + " With the lable: " + vssVersion.Label);
}
}
}
}
}

number changed automatically to the string

I am trying to read Excel file through apache poi. In a column I am having the data `"9780547692289". After iterating all the columns, in the output, the number is displaying like this "9.780547692289E12". I mean number changed automatically to the string(because it has'E'). I have to keep this as number only(as it is). What should I do..?
It is probably just a display setting. The "E" is just for scientific notation for displaying the number.
//while getting values from excel u can use this method
private String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return cell.getNumericCellValue() + "";
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() + "";
}else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
return cell.getErrorCellValue() + "";
}
else {
return null;
}
}
This worked for me
if (cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA )
{
int i = (int)cell.getNumericCellValue();
String cellText = String.valueOf(i);
}