In my app the users are able to select articles to download using different criteria. One of them is year and month. For this I would like an AlertDialog with a list of years. If the user then clicks on a year, the list will expand and show january, february etc.
I know how to make an expandable listview using a SimpleExpandableListAdapter but that is not what I want. Since the other criteria (eg. category) are also list AlertDialogs, I want something that is similar in look and feel.
Is it possible to accomplish such an expandable list AlertDialog?
SOLUTION
This is what I ended up with based on CommonsWare's solution:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");
ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);
builder.setView(myList);
AlertDialog dialog = builder.create();
dialog.show();
Only problem remaining: how do I implement the onClick listener for the AlertDialog? Normally I would do it in the setItems() method, but am not using setItems.
I added myList.setOnItemClickListener after myList.setAdapter() but it is ignored. Nothing happens when I click an item:
myList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
try {
Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
}
catch(Exception e) {
System.out.println("something wrong here ");
}
}
});
Solution to click problem:
The solution was quite simple. Since it is an expandable list, item clicks are captured by the list itself to open the child elements. Thus, the event handler is never called.
Instead you have to implement OnChildClickListener() that - as the name suggests - listens to child clicks!