Uiautomator- how to get the text value on the textview obj of which the checkbox value is checked??

5.6k views Asked by At

I am trying to get the textview value of the linearlayaout having the checkbox with an (ischecked == true) property. Basically trying to get the text of all the items whose corresponding checkbox is checked.

->ListView
   -> LinearLayout
       -> Checkbox
       -> textview
       -> textview

I tried doing the following:

     UiScrollable citylistView = new UiScrollable(new UiSelector());
     listView.setMaxSearchSwipes(100);
     listView.scrollTextIntoView(name);
     listView.waitForExists(5000);
     UiObject listViewItem = listView.getChildByText(new UiSelector()
        .className(android.widget.CheckBox.ischecked(true));
     String cityName =listViewItem.gettext();

But this doesn't seem to work. I'd really appreciate if anyone could point out what I'm doing wrong. Thanks for your help!

2

There are 2 answers

1
Marc Cork On

From what I can see is

.className(android.widget.CheckBox.ischecked(true));

is incorrect as that function is expected a string. I think you need to rewrite it like this.

UiObject checkBox = listView.getChildByText(new UiSelector()
    .className("android.widget.CheckBox");
if(checkBox.isSelected()){
   String cityName = listView.getChildByText(new UiSelector()
    .className("android.widget.TextView").gettext();
}

If I am correct this should help with your solution, it would be useful for a screen shot of the section and the xml, be easier to know how it is directly laid out. Unfortunately I couldn't test this solution but I hope it helps.

0
Allen Hair On

There are a few problems with your code:

  • The selector on the first line will match any element. This probably isn't what you want. Also, based on the next few lines it looks like, citylistView should be listView?
  • Lines 5-6 won't compile. The UiSelector.className(..) method takes a String or a Class object, so it should either be .className("android.widget.CheckBox") or .className(android.widget.CheckBox.class)

  • Also on line 6 you're missing a closing parenthesis before calling isChecked(true). Instead of .className(android.widget.CheckBox.ischecked(true)) it should be .className(android.widget.CheckBox.class).ischecked(true)