Pierre Hébert and all other Java pros: Is there a way to sort items into categories in an AlertDialog?
Pierre Hébert and all other Java pros: Is there a way to sort items into categories in an AlertDialog?
For example:
Header 1
Item 1
Item 2
Header 2
Item 3
Item 4
Item 5
Header 3
Item 6
]]>« Something I had in mind… (Previous Post)
(Next Post) The Zooper widget scaling seems to work really good. »
< ![CDATA[
You know more than me in those java features.
Searching in Google…
What about Expandable List View? (Showing it in an alert)
http://developer.android.com/reference/android/widget/ExpandableListView.html
]]>
< ![CDATA[
I love this one : https://github.com/emilsjolander/StickyListHeaders
]]>
< ![CDATA[
Sadly it’s not possible to import such a library
]]>
< ![CDATA[
oh yes that’s true :/
]]>
< ![CDATA[
Wow this stuff is complicated… I need not only a listview, but also a matching adapter and several matching objects containing the data…
]]>
< ![CDATA[
yes ! I took a lot of time to understand it well !!
]]>
< ![CDATA[
Here is the better tutorial I found http://www.vogella.com/tutorials/AndroidListView/article.html
]]>
< ![CDATA[
An easy example is http://goo.gl/0x2ndV (from the tuto)
]]>
< ![CDATA[
Next Question: How do I bind a Class requiring types?
My Code so far (not working):
LL.bindClass(“android.app.AlertDialog”);
LL.bindClass(“android.content.DialogInterface”);
LL.bindClass(“android.widget.ExpandableListView”);
LL.bindClass(“android.widget.SimpleExpandableListAdapter”);
LL.bindClass(“android.support.v4.util.SimpleArrayMap“);
var builder=new AlertDialog.Builder(LL.getContext());
var view=new ExpandableListView(LL.getContext());
var map=new SimpleArrayMap();
map.put(“Header 1″,”Item 1”);
map.put(“Header 1″,”Item 2”);
map.put(“Header 1″,”Item 3”);
map.put(“Header 2″,”Item 4”);
map.put(“Header 3″,”Item 5”);
map.put(“Header 3″,”Item 6”);
var adapter=new SimpleExpandableListAdapter(LL.getContext(),map,null,null,null,null,null,null,null);
view.SetAdapter(adapter);
builder.setView(view);
builder.setCancelable(true);
builder.setTitle(“test”);
builder.setNegativeButton(“Cancel”,new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.cancel();}});
builder.show();
]]>
< ![CDATA[
Pierre Hébert is this even possible?
]]>
< ![CDATA[
Generic classes such as SimpleArrayMap are not like templates in C++. The class name is only SimpleArrayMap without the parameterized types: they are removed at compile time.
But I would think that android.support.v4.util is not available: if I am not mistaken, it is part of a library built into the app, and it is not available in the android framework itself. As LLX does not include this library, I don’t think it should be available.
Instead of SimpleArrayMap, use HashMap as a drop in replacement.
]]>
< ![CDATA[
Pierre Hébert Sorry to ask you always, but I need to create a SimpleExpandableListAdapter (http://developer.android.com/reference/android/widget/SimpleExpandableListAdapter.html) and for the constructor I need resource identifier. How do I use that with LLX?
(Best for me would be if you provide example code)
BTW: got hashmap working, that’s not a problem.
]]>
< ![CDATA[
Using LL.bindClass(“android”) should allow the use of android.R.layout.something in JavaScript.
But I will try to provide you a sample code. SimpleExpandableListAdapter is not as Simple as its name says 🙁
]]>
< ![CDATA[
I noticed that 😀
]]>
< ![CDATA[
Based on your sample code (the only missing part was the data stuff):
(I strongly suggest to make a function to populate groupData and childData, as seen on http://stackoverflow.com/questions/18350824/how-should-be-the-data-for-a-simpleexpandablelistadapter-organized)
LL.bindClass(“android.app.AlertDialog”);
LL.bindClass(“android.content.DialogInterface”);
LL.bindClass(“android.widget.ExpandableListView”);
LL.bindClass(“android.widget.SimpleExpandableListAdapter”);
LL.bindClass(“java.util.HashMap”);
LL.bindClass(“java.util.ArrayList”);
LL.bindClass(“android.R”);
var builder=new AlertDialog.Builder(LL.getContext());
var view=new ExpandableListView(LL.getContext());
var groupData = new ArrayList();
var gd1 = new HashMap();
gd1.put(“root”, “Group 1”);
var gd2 = new HashMap();
gd2.put(“root”, “Group 2”);
groupData.add(gd1);
groupData.add(gd2);
var childData = new ArrayList();
var cd1 = new ArrayList();
var cd1m1 = new HashMap();
cd1m1.put(“child”, “Child 1 in group 1”);
cd1.add(cd1m1);
var cd1m2 = new HashMap();
cd1m2.put(“child”, “Child 2 in group 1”);
cd1.add(cd1m2);
childData.add(cd1);
var cd2 = new ArrayList();
var cd2m1 = new HashMap();
cd2m1.put(“child”, “Child 1 in group 2”);
cd2.add(cd2m1);
childData.add(cd2);
var adapter=new SimpleExpandableListAdapter(LL.getContext(),
groupData,
R.layout.simple_expandable_list_item_1,
[“root”],
[R.id.text1],
childData,
R.layout.simple_expandable_list_item_1,
[“child”],
[R.id.text1]
);
view.setAdapter(adapter);
builder.setView(view);
builder.setCancelable(true);
builder.setTitle(“test”);
builder.setNegativeButton(“Cancel”,new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.cancel();}});
builder.show();
That’s the miracle of Open Source.
]]>
< ![CDATA[
Here is a working function for an expandable list:
LL.bindClass(“android.app.AlertDialog”);
LL.bindClass(“android.content.DialogInterface”);
LL.bindClass(“android.widget.ExpandableListView”);
LL.bindClass(“android.widget.SimpleExpandableListAdapter”);
LL.bindClass(“java.util.HashMap”);
LL.bindClass(“java.util.ArrayList”);
LL.bindClass(“android.R”);
//function to display a grouped list where the user can select one item
//items should be an array containing arrays which first item is the group and the second item is an array of the items in this group
//onClickFunction has to have two arguments. first is group position, second is child position
function expandableList(items,onClickFunction,title)
{
var builder=new AlertDialog.Builder(LL.getContext());
var view=new ExpandableListView(LL.getContext());
//transform array of items into the correct format
var groupData=new ArrayList();
var childData=new ArrayList();
for(var x=0;x
{
var gd=new HashMap();
gd.put(“root”,items[x][0]);
var cd=new ArrayList();
groupData.add(gd);
for(var y=0;y
{
var cdMap=new HashMap();
cdMap.put(“child”,items[x][1][y]);
cd.add(cdMap);
}
childData.add(cd);
}
//assign the items to an adapter
var adapter=new SimpleExpandableListAdapter(LL.getContext(),groupData,R.layout.simple_expandable_list_item_1,[“root”],[R.id.text1],childData,R.layout.simple_expandable_list_item_1,[“child”],[R.id.text1]);
//set function to run on Click to listener
var listener=new ExpandableListView.OnChildClickListener()
{
onChildClick:function(parent,view,groupPosition,childPosition,id)
{
dialog.dismiss();
setTimeout(function(){onClickFunction(groupPosition,childPosition);},0);
return true;
}
}
//assign adapter and listener to listview
view.setAdapter(adapter);
view.setOnChildClickListener(listener);
//finish building
builder.setView(view);
builder.setCancelable(true);
builder.setTitle(title);
builder.setNegativeButton(“Cancel”,new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.cancel();}});
dialog=builder.create();
dialog.show();
}
]]>