Anyone can explain why ItemClick event does not work with GridView in AlertDialog?

Anyone can explain why ItemClick event does not work with GridView in AlertDialog? It should, googled, all samples are like this. In pure Java, though… Dialog shows properly, can click, but no event is fired.

var llCtx=LL.getContext();

var adapter=new ArrayAdapter(llCtx, R.layout.simple_list_item_1, items);

var gridView=new GridView(llCtx);

gridView.setAdapter(adapter);

gridView.setNumColumns(4);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

onClick:function(parent,v,position,id)

{

Android.makeNewToast(id+”/”+position,true).show();

}

});

var builder=new AlertDialog.Builder(llCtx);

builder.setView(gridView);

builder.setTitle(title);

builder.show();

]]>

14 Commentsto Anyone can explain why ItemClick event does not work with GridView in AlertDialog?

  1. Anonymous says:

    < ![CDATA[

    I think you should add the on click on the adapter var (the ArrayAdapter)

    ]]>

  2. Anonymous says:

    < ![CDATA[

    Benoît de Chezelles


    No, the parent is normally the correct position for a listener. Adapters don’t even allow listeners by default.

    ]]>

  3. Anonymous says:

    < ![CDATA[

    Lutz Linke Your mistake is basic: the method is called onItemClick, not onClick.

    ]]>

  4. Anonymous says:

    < ![CDATA[

    Lukas Morawietz Bwaaaahhh, it really was that simple! Thanks a million!

    ]]>

  5. Anonymous says:

    < ![CDATA[

    Need some more help.


    When I use “var adapter=new ArrayAdapter(llCtx, R.layout.simple_list_item_1, items) {};” (with curly brackets; want to override getView) I get “cannot convert null to int” on line gridView.setAdapter.


    I think it’s because ArrayAdapter is a generic type. Is there any way to instantiate it as i.e. ArrayAdapter in JavaScript to hopefully solve the problem? Or do I need to use a different adapter type? Like inheriting somehow from BaseAdapter directly?



    Lukas Morawietz​, can you help me out? Thanks in advance.

    ]]>

  6. Anonymous says:

    < ![CDATA[

    Try a SimpleAdapter. Generic classes are an unsolved problem in js.

    ]]>

  7. Anonymous says:

    < ![CDATA[

    Lukas Morawietz Thanks. Will try. Need to figure out how to create a proper Map (also generic construct) like array in JavaScript. Did you successfully use SimpleAdapter and could please give a sample?

    ]]>

  8. Anonymous says:

    < ![CDATA[

    I used a SimpleExpandableListAdapter around line 600 in my multitool. The construct might be similar. The generic version is no problem here, because the map maintains its type automatically. Generic types seem to be removed here at runtime.

    ]]>

  9. Anonymous says:

    < ![CDATA[

    Lukas Morawietz​ Tried, did not work. Seems it’s not possible to inherit and override because of generics in constructor.



    Or am I doing something really wrong? Also tried creating own inherited adapter class.



    var adapter=new SimpleAdapter(llCtx, groupData,R.layout.simple_list_item_1,[“root”],[R.id.text1]){


    getView:function(position, convertView, parent)


    {


    // do something


    }


    };

    ]]>

  10. Anonymous says:

    < ![CDATA[

    LL.bindClass(“android.widget.SimpleAdapter”);


    LL.bindClass(“android.R”);


    LL.bindClass(“java.util.ArrayList”);


    LL.bindClass(“java.util.HashMap”);


    var list = new ArrayList();


    var map = new HashMap();


    map.put(“root”,”test”);


    list.add(map);


    var adapter=new SimpleAdapter(LL.getContext(), list,R.layout.simple_list_item_1,[“root”],[R.id.text1]);



    The problem is not the generic type, but the extension you wanted to make. The code above works.



    The bigger problem I see here is that we can only use android Resources.



    Pierre Hébert would it be possible somehow to create custom resources for scripts?

    ]]>

  11. Anonymous says:

    < ![CDATA[

    Lukas Morawietz  Maybe, but not sure. Try with something like this:



    var pkg = “your_package_name”;


    var remoteContext = createPackageContext(pkg, 0);


    var resources = remoteContext.getResources();


    int identifier = resources.getIdentifier(“your_layout_name”, “layout”, pkg);



    Then make sure you use remoteContext when  inflating the layout.




    Lutz Linke  Regarding generics, as Lukas said I don’t think this is the issue because type data are erased at build time. Hence a “Blah” becomes a “Blah”.

    ]]>

  12. Anonymous says:

    < ![CDATA[

    Lukas Morawietz Pierre Hébert​


    Sorry, forgot to mention the positive case. Yes, SimpleAdapter and ArrayAdapter work, like “new Adapter(…);”. Was able to create a HashMap and ArrayList, thanks.



    But just add opening and curly brackets like “new Adapter(…) {};” and it fails, even if not adding anything inside. And that is kinda strange. So it’s not possible to create overrides I’m afraid.



    Might only try to somehow inherit from BaseAdapter and then implement everything manually. If possible at all…

    ]]>

  13. Anonymous says:

    < ![CDATA[

    Lutz Linke new ArrayAdapter(LL.getContext(), 0) {}; works, so I believe the problem is elsewhere. I will try  with a more complete sample to see what the problem is exactly, but later as I am busy at the moment.

    ]]>

  14. Anonymous says:

    < ![CDATA[

    Pierre Hébert  The problem seems to be that what you put into the curly brackets isn’t changed on the new instance, but instead supplied as additional argument.

    ]]>

Leave a Reply

Your email address will not be published. Required fields are marked *