Hi I am setting some text inside a RobotoText that is positioned inside of a ViewHolder I am calling it like this:
viewHolder.txtSimilarAds.setText((((Property) ads.get(position)).getSimilar_items_count() == 1 ? context.getString(R.string.ad_data_similar) : context.getString(R.string.ad_data_similar_plural, ads.get(position).getImagesCount())));
However (sometimes NOT always)for some reason I keep getting this error exception UnknownFormatConversionException and it points to this line inside of the class. What could be the problem? Am I doing anything wrong?
Check that the string in R.string.ad_data_similar_plural contains a valid placeholder for an integer. It should be something like "Here is my number: %d".
String format specification
As an aside, one-liner like these are harder to understand, and make debugging more difficult. A more readable approach would have given the erroneous line more easily:
String similarAdsText;
Property adsProperty = ads.get(position);
if (adsProperty.getSimilar_items_count() == 1) {
similarAdsText = context.getString(R.string.ad_data_similar);
}
else {
similarAdsText = context.getString(R.string.ad_data_similar_plural, adsProperty.getImagesCount());
}
viewHolder.txtSimilarAds.setText(similarAdsText);
You are trying too many methods in single line try splitting it. The problem looks like you are comparing Property object with 1 which is wrong.
(Property)ads.get(position)).getSimilar_items_count() == 1
change this to
Property property = (Property) ads.get(position));
property.getSimilar_items_count() == 1
Related
I have a 2D array (matrix) of Textviews called Board. Board has 16 Textviews in it, only one of them is invisible. In this given lines of code, I tried to find the invisible one between all the rest. For some reason, the line with the If condition collapses my app every time. I don't understand what my problem is, can someone help me?
P.S. Sorry for my English, it's not my native language.
Here is my code:
var i = 0
for (i in 0..4) {
var j = 0
for (j in 0..4) {
var tvtemp = board[i][j]
if (tvtemp.visibility == View.INVISIBLE) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}
}
}
== operator is used to compare the data of two variables.
Please don’t misunderstand this equality operator with the Java == operator as both are different. == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references.
Solution:
if(!tvtemp.isVisible){
....
}
You could try using the isShown method. It determines if the view is visible to the user. If a view's visibility is set to be visible, but it it crossed off the screen then isShown will equal false. If you have no scrollview and all views are fitted in the activity then this could work for you
if (!tvtemp.isShown) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}
This question already has answers here:
TextViews text change in a for loop
(4 answers)
Closed 4 years ago.
I need to set the text of some textViews already existing in a layout with a for loop. Eg. TextView_01, TextView_02, etc. IS there a way to do something like the following speculative code:
for(1 in 0..6){
TextView_0(change value with i).text = something
}
This isn't the best way to do things, but it's probably the most universal, while avoiding creating a pre-defined array of TextViews:
val base = "TextView_0"
for (i in 1 until 6) {
val textView = findViewById(resources.getIdentifier("${base}i", "id", packageName)
textView.text = something
}
I changed your for loop a little bit, since you had the wrong syntax. I also replaced .. with until, since .. means through the right bound, which probably isn't what you want. If you do need 6 to be a value of i, then change it back to ...
If all the TextViews are under a single parent in XML, give that parent an ID, then loop through its children:
val parent = findViewById(R.id.tvParent)
for (i in 0 until parent.getChildCount()) {
(container.getChildAt(i) as TextView).text = something
}
You can use parent container
for (i in 0 until container.childCount) {
(container.getChildAt(i) as TextView).text = something
}
A better way is to make use of DataBinding and LiveData APIs, you can assign different variables or the same variable to your TextViews' text attributes.
I'm testing a hybrid app, where each view has a web view.
In one of these web views I have a list of elements with the same attribute. They have the same xpath locator that is something like:
//h4[contains(#data-role, 'product-name')]
I want to create a list of these elements and iterate through them, count them, get their attributes.
In the documentation, I found two similar methods:
findElement(locator, value)
and
findMultipleElements(locator, value)
Though it's totally unclear to me how to use it. I tried to find examples on it but with no success.
Could someone help me with this?
Here is the solution that I have found.
#kaqqao is right that findMultipleItems call returns Atom<List<ElementReference>> that is not usable with onWebView() because there you have only withElement() that accepts either Atom<ElementReference> or just ElementReference
What you can do though is perform your action that find multiple items and just get results from your Atom. This is how it works internally if you check the source of doEval method inside Web.java for espresso.
val elements = with(AtomAction(findMultipleElements(
Locator.XPATH,
"YOUR_COMPLEX_XPATH"
), null, null)) {
onView(ViewMatchers.isAssignableFrom(WebView::class.java)).perform(this)
this.get()
}
This code will give you List<ElementMatcher>.
Then just run it as
elements.forEach {
onWebView().forceJavascriptEnabled().withElement(it).perform(webClick())
}
Can you try something like that? Since what you should care about is really the ElementReference and you can iterate the lsit returned from findMultipleElements with simple for/foreach statement:
yourList = findMultipleElements(locator, value);
yourList.size(); //this will get you the count of found elements with that locator
for(Atom<ElementReference> item : yourList ){
item.getAttribute...
//and whatever you want
}
I want to fetch all possible InputTypes available for EditText dynamically.
I have checked out couple of links, I know how to setInputTypes through xml or dynamically, how to fetch all types of InputTypes?
I have checked the InputType.class too, didnt't find any such method in it. Still is it possible to get all the InputTypes?
Thanks
I can't imagine why you need this, and probably there is a better solution to your problem, but here is an example using reflection, sans the exception handling:
for (Field f : InputType.class) {
nt mod = f.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
//do something with this
f.getName();
}
}
So I'm trying to make a calculator app (just to get a hang of android development), and I noticed with some testing that the parseInt conversion from "String tack1" is causing the app to crash. Can someone tell me why? I can't seem to figure it out and I've been searching the internet for quite a while now. (hehe I'm a noob so please go easy) In the code below, I've modified a couple lines to make it seem obvious what it's supposed to print however it still crashes. Here's the code:
equals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oC = 1; //operator checker is on 1 (for plus)
switch(oC){ //check for operator clicked
case 0: break;
case 1:
tack1 = "1"; //INSERTED THIS TO MAKE OUTPUT OBVIOUS
tack1 = tack1.trim(); tack2 = tack2.trim(); //INSERTED BEFORE TO DEAL WITH WHITESPACE
numOne = Integer.parseInt(tack1); //CRASHES HERE
answer.setText(numOne);
modeChecker = 0; oC = 0;break;
NOTES ON PROGRAM(some of comments repeated and other stuff as well):
The tack1 = "1"; is to make output obvious
The tack1.trim() is to deal with whitespace
Yes whatever is in tack is a number and an integer (not even a negative integer)
Yes numOne is an integer and is defined wayy above (not in code listed here)
Sorry the indents are all messed up(after case 1) because of the comments I added
This is a section of my onClick method, so closing brackets aren't included in here.
Can someone please help me?
THANKYOU :D
I'd be willing to bet it's actually crashing on the following line AFTER the call to parseInt.
You're calling setText(int) on your TextView. When you pass an int to this method, that int is a pointer to a string resource...you're probably expecting an auto-conversion to a string. Since you are passing it an int that is generated in your application, it's extremely unlikely that this int also points to a string resource in your res/values/strings.xml file. What you really want to do is change numOne to a string first, which you can do inline:
Change
answer.setText(numOne);
to
answer.setText(String.valueOf(numOne));
and you're good to go.