qpython

Qpython3 and androidhelper, droid.dialogSetSingleChoiceItems


I am trying to figure out androidhelper, which I find very useful to work with python 3 and Android. I am trying to return a selection from a list using dialogSetChoiceItems. I have it set up, and I have tried various ways of going about it. I will get something returned, but it is the length of the list. Any help would be appreciated. Thanks,

#-*-coding:utf8;-*-
#qpy:3 
#qpy:console
import androidhelper
droid = androidhelper.Android()

def test_alert_dialog_with_single_choice_list(Title, ListOfStuff):

  droid.dialogCreateAlert(Title)
  droid.dialogSetSingleChoiceItems(ListOfStuff)

  droid.dialogSetPositiveButtonText('Select')
  droid.dialogShow()
  response = droid.dialogGetSelectedItems().result
  return response





if __name__ == '__main__':

    Listy = [1,2,3,4,5,6,7,8]
    YTitle = 'Title of The Thing'

    FReturn = test_alert_dialog_with_single_choice_list(YTitle, Listy)

    print(FReturn)

Solution

  • This example makes a dialog with "xyz" buttons and "abcde" items, then print(pressed_button, selected_items_indexes), and makeToast your pressed button and selected items. Try it! It worked fine on my phone, tell me if it doesn't on your phone.

    #-*-coding:utf8;-*-
    #qpy:2
    #qpy:console
    import androidhelper
    droid=androidhelper.Android()
    def mydialog(title,buttons=["OK"],items=[],multi=False):
        title = str(title)
        droid.dialogCreateAlert(title)
        if len(items) > 0:
            if multi:
                droid.dialogSetMultiChoiceItems(items)
            else:
                droid.dialogSetSingleChoiceItems(items)
        if len(buttons) >= 1:
            droid.dialogSetPositiveButtonText(buttons[0])
        if len(buttons) >= 2:
            droid.dialogSetNeutralButtonText(buttons[1])
        if len(buttons) >= 3:
            droid.dialogSetNegativeButtonText(buttons[2])
        droid.dialogShow()
        res0 = droid.dialogGetResponse().result
        res = droid.dialogGetSelectedItems().result
        if "which" in res0.keys():
            res0={"positive":0,"neutral":1,"negative":2}[res0["which"]]
        else:
            res0=-1
        return res0,res
    items = [i for i in "abcde"]
    btns = [i for i in "xyz"]
    pressed_button, selected_items_indexes = mydialog("Dialog test!", btns, items, True)
    print(pressed_button, selected_items_indexes)
    if pressed_button == -1:
        droid.makeToast("You didn't press any buttons!")
    else:
        droid.makeToast("You pressed %s!"%("xyz"[pressed_button]))
    if selected_items_indexes == []:
        droid.makeToast("You selected nothing!")
    else:
        selected_abc = "".join(["abcde"[i] for i in selected_items_indexes])
        droid.makeToast("You selected %s!"%selected_abc)