Grab checkbox name at runtime in android - java

I am looping through a list of checkboxes upon click of a button. What I am looking to do is grab the name of the checkbox at runtime to strip out an integer value specified within the name. I am not looking to get the value nor the id. So in the strings.xml file, <string name="checkList1">Pain assessment.</string>, I am trying to get checkList1 at run time. I can get the text without a problem. Currently I am looping through the view elements with the code below:
RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);
for (int i = 0; i < root.getChildCount(); i++)
{
View v1 = root.getChildAt(i);
Class c = v1.getClass();
if (c == CheckBox.class)
{
CheckBox thisBox = (CheckBox)v1;
if (thisBox.isChecked())
{
String text = (String)thisBox.ge;
DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
countItemsFinished++;
}
checkBoxCount++;
}
}
What I am looking for is to somehow get the name of Checkbox thisBox. So when it loops through and hits the Pain Assessment checkbox, I want to be able to pull out checkList1. Without going as far as ripping through the strings.xml file based on the text I find to get the name, I was hoping maybe there was a simpler solution that I maybe overlooking.
Thank You in advance.

CheckBox extends from TextView so to retrieve a text from it is quite simple :
String text = thisBox.getText().toString();
http://developer.android.com/reference/android/widget/CheckBox.html
If you want to retrieve the key name of the string. I suggest you put it into the tag of the object :
thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);
Retrieve it like that :
String text = (String)thisBox.getTag();
that should do the trick.

Related

Failed to get string from autocompletebubbletext

i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!

How to add pre defined text in getText() from Textview and show the result

I have textview where user are asked to enter some information and that information is uploaded in Firebase Data Structure and then is Displayed on another activity
Here is the code I'm using to getText from Textview
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = etAuthor.getText().toString();
and it does job pretty well.
and it is added to firebase.
But what if I want to add predefined Text like
Author:getText()
Here I have added author.
and This will be upudated on Database as well and will Displayed to user
instead of Consider Author is
J. K. Rowling
It will show
Author:J.K Rowling
Any help is appreciated.
Thanks in advance.
you can concatenate the desired string, in you case:
String bauthor ="Author:"+etAuthor.getText().toString();
String bauthor = "Auther : "+etAuthor.getText().toString(); //use this, '+' use for concatenate
#Bir Nepali, you have to just append while getting the text.
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = "Author:" + etAuthor.getText().toString();
Now you can populate this bauhtor in the view.

Unable to click element via Xpath but can find it via ID

I am trying to delete every row in a table untill there are none left, only then do I want to continue with the rest of my test case. For this reason I'm using an if/else statement. As part of this statement I need to select the first row, click the delete button and then confirm my action by clicking OK. The problem is in this last action. I can find the OK via the findElement by.id but not via class and text value. Problem is that the ID is only reliable for deleting the first row as the application uses auto generated ID's. That is why I'm trying to find the OK via it's class and text value.
if(driver.findElement(By.xpath("first row of the table")) != null)
{
driver.findElement(By.xpath("first row of the table")).click(); //select row
driver.findElement(By.xpath("//*[#type='button' and text()='Delete']")).click(); //click delete button
wait.until(ExpectedConditions.elementToBeClickable(By.id("ext-gen483"))); //wait untill the OK button (id=ext-gen483) in dialog box is visible to confirm delete
/** this section is built in to find out why I cant find the OK button. First I catch and print the class and text for the OK button via the id. */
String Klasis = driver.findElement(By.id("ext-gen483")).getAttribute("Class");
System.out.println("Value of Class = "+Klasis);
String Tekstis = driver.findElement(By.id("ext-gen483")).getText();
System.out.println("Value of Text = "+Tekstis);
String xpathOK = "//*[#class='"+Klasis+"'"+" and text()='"+Tekstis+"']";
System.out.println(xpathOK);
driver.findElement(By.xpath(xpathOK)).click();
}
else
The print actions result in:
Value of Class = x-btn-text
Value of Text = OK
//*[#class=' x-btn-text' and text()='OK']
But the button cant be clicked: ElementNotVisibleException: Element is not currently visible and so may not be interacted with
However, if I use the following code to click the OK, it works just fine:
driver.findElement(By.id("ext-gen483")).click();
This is the line of code representing the OK button:
<button class=" x-btn-text" id="ext-gen483" type="button">OK</button>
As a result of a suggestion that the extra space in the class might be getting in the way I changed the code so that it no longer uses the class but the button attribute:
String Klasis = driver.findElement(By.id("ext-gen483")).getAttribute("Class");
System.out.println("Value of Class = "+Klasis);
String Tekstis = driver.findElement(By.id("ext-gen483")).getText();
System.out.println("Value of Text = "+Tekstis);
String Typeis = driver.findElement(By.id("ext-gen483")).getAttribute("Type");
System.out.println("Value of Type = "+Typeis);
String xpathOK = "//*[#type='"+Typeis+"'"+" and text()='"+Tekstis+"']";
System.out.println(xpathOK);
//driver.findElement(By.id("ext-gen483")).click();
driver.findElement(By.xpath(xpathOK)).click();
The print actions result in:
Value of Class = x-btn-text
Value of Text = OK
Value of Type = button
//*[#type='button' and text()='OK']
But it still fails on the last line with the same message:ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Edit
there are 2 buttons that comply to my findelement statement. If I then choose to click the second it works!
List<WebElement> listOfOKbut = driver.findElements(By.xpath("//*[#class=' x-btn-text' and text()='Ja']"));
System.out.println("aantal ja knoppen:"+listOfOKbut.size()); if(listOfOKbut.size() >= 2) {
listOfOKbut.get(1).click();
}
try using contains instead of "=" for the #class part:
String xpathOK = "//*[contains(#class,'x-btn-text') and text()='OK']";
To find an element more accurately use the following methods
String xpath = "//button[contains(#class,'x-btn-text') and text()='OK']"
Find the parent element of button whose class or id is constant.
xpath = "//parent//button[contains(#class,'x-btn-text') and text()='OK']"

Return getString from xml with a string variable?

I am newcomer to programming and I am attempting to create an Android app using Android Studio. I've tried searching but my findings do not appear to be what I am looking for, because they seem to be overly complex. What I've written below is just an example.
I want to be able to return a string from string.xml when user types "whale". The string in this case is information about the whale.
This is my java file, animal is already a string entered from a form.
TextView textview = new TextView(this);
String animalType = "water_" + animal; // This become water_whale if user typed whale
String animalInfo = getString(R.string.animalType); // This doesn't work
textView.setText(animalInfo);
This is my string.xml
<string name="water_fish">Fish is a small bla...</string>
<string name="water_whale">A whale is an enourmous blabla...</string>
<string name="land_giraffe">Africa.</string>
I have probably tunneled on this particular way and I have probably miss something obvious or is there another way to do this?
R.string.anyIdentifer represents an integer value. You can't add your own identifier with it, just the way you can't call any non existent property on any class. If you want to access any resource dynamically with it's name then there is a different approach for that.
Use this
TextView textview = new TextView(this);
String animalType = "water_" + animal;
int animalTypeId = getResources().getIdentifier(animalType, "string", getActivity().getPackageName())
String animalInfo = getResources().getString(animalTypeId);
textView.setText(animalInfo);
String string=getResources().getString(R.string.water_whale);
you can't use getString() method directly.

How to not add a string to a text area if it contains something - Java

I have the following:
InputStream inputFromServer
TextArea t
String display
String validation
String display holds the data from the inputstream, it then adds it to the text area. I want to check that if display contains validation then dont write it to the text area. Here is the code I currently have:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
t.setText(s);
}
I want something like:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
if display contains validation {
THEN DONT WRITE IT TO THE TEXT AREA
}
else{
t.setText(s);
}
}
If the method is completely wrong then can someone guide me in the right direction please? Thanks
You could do like this:
while ((inputFromServer.read())!=-1)
{
display = display + ((char)inputFromServer.read());
if (!display.contains(VALIDATION_STRING) {
t.setText(s);
}
}
Ok since the validation String is possibly uninitialized you can check against that
display += ((char)inputFroMServer.read());
if(validationString.equals("") || !display.contains(validationString)) {
t.setText(display);
}
Or like I said in my comment you can do:
!display.toLowerCase().contains(validationString.toLowerCase())
if you don't care about case
Also if you are going to editing the display string a bunch of times you should consider using a StringBuilder

Categories

Resources