I have an AlertDialog populated by an ExpandableListView. The list itself works perfectly, but for some reason clicks are ignored. Apparently my click handler is never called.
This is the code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");
ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);
myList.setOnItemClickListener(new ExpandableListView.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 ");
}
}
});
builder.setView(myList);
dialog = builder.create();
dialog.show();
If I instead try to populate the AlertDialog with a plain listview, click events are generated without problems:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
modeList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
builder.setView(modeList);
AlertDialog dialog1 = builder.create();
dialog1.show();
What could be the reason why click event fails in my ExpandableListView but not in a normal ListView? I am probably missing something, but I have no idea what that could be.
Ok, 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!
Thx, I was also wondering about it. But still interesting is info in documentation:
public void setOnItemClickListener (AdapterView.OnItemClickListener l)
"Register a callback to be invoked when an item has been clicked and the caller prefers to receive a ListView-style position instead of a group and/or child position.".
In docs there is clearly mentioned that you should be able to set and handle this listener if you want...
You can actually use
expandableListView.setOnGroupExpandListener to catch the event when expanding
expandableListView.setOnGroupCollapseListener to catch the event when collapsing.
expandableListView.setOnGroupClickListener to catch the event both when expanding or collapsing
link to developer site:
https://developer.android.com/reference/android/widget/ExpandableListView.html
Related
I need to create an AlertDialog with multiple choice items but I'm having some trouble trying to set a custom layout file to the internal ListView.
For single choice items I use a constructor that takes a ListAdapter as parameter and this way I can set the proper layout resource for each row:
builder.setSingleChoiceItems(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_single_choice_answer, items), checkedItem,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkedItem = which;
toggleEditTextAnswer(checkedItem == (items.length - 1));
dialog.dismiss();
}
});
The problem is that there's no constructor for setMultiChoiceItems that accepts a ListAdapter as parameter when creating a multiple choice list.
I need to set a custom layout for each row because I use drawable selectors for setting the row background and text color.
Any ideas?
PS. here is the AlertDialog source code for more information.
https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java
Well, I know I should create a custom Dialog but right now I don't have the time to do it ... so this is how I hacked this problem:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the adapter
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
checkedItems[position] = !checkedTextView.isChecked();
}
});
listView.setDivider(null);
listView.setDividerHeight(-1);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
setCheckedItems(((AlertDialog) dialog).getListView());
}
});
alertDialog.show();
First I set the adapter with the items and the instead of calling setMultiChoiceItems I get the ListView object from the Dialog and then configure it myself.
I would recommend that you create your own dialog class like this:
Customizing dialog by extending Dialog or AlertDialog
How to create a Custom Dialog box in android?
This way you will have full control over your dialog and you can customize it any way you want.
Also if you still have issues with your list view after that you can customize your list view items completely: (You can only affect the background and text in a small way through xml and selectors without doing your custom implementation)
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Try it out, it may seem hard but when you do it once it will be piece of cake and will do wonders for you in your future development projects.
I've tried to create a list view dialog to display a list of choose. My code is shown below:
LayoutInflater factory=LayoutInflater.from(this);
final View stuckLevelDialogView=factory.inflate(R.layout.report_stuck_dialog, null);
final ListView stuckLevelListViewForDialog=(ListView)stuckLevelDialogView.findViewById(R.id.report_stuck_dialog_listview);
final String[] stuckLevelList=new String[]{"1 - You can move freely","2 - You have to be aware of your movement","3 - You can move slowly","4 - There is a traffic jam","5 - There is a serious traffic jam"};
ArrayAdapter<String> adapterForDialog=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stuckLevelList);
stuckLevelListViewForDialog.setAdapter(adapterForDialog);
final AlertDialog.Builder stuckLevelDialog=new AlertDialog.Builder(this);
stuckLevelDialog.setTitle("What stuck level is this point?");
stuckLevelDialog.setView(stuckLevelDialogView);
stuckLevelDialog.show();
However, when I choose an option, the onItemClick is executed, but the listview dialog doesn't disappear, I have to press back button manually. I've tried to debug the code for a whole day, but it has not been solved yet. Please help me. Thank in advanced!
I think you need to dismiss() the dialog in your onItemClick listener as below:
stuckLevelListViewForDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
int p_arg2, long p_arg3) {
stuckLevelDialog.dismiss();
}
});
Use stuckLevelDialog.dismiss; at the end of onItemClick.
You can set setSingleChoiceItems in your alert dialog box with your items list, which will show a list with a radio buttons. If you want to add buttons you can else once user select any items you can dismiss the dialog.
new AlertDialog.Builder(this)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// here you can do your functionality and can dismiss dialog as well
dialog.dismiss();
}
})
.show();
I am experiencing a odd problem. My OnItemSelectedListener seems only works one time, i mean that it shows my test Toast at the first time when clicking the coresponding items, but it doesn't show the test Toast when i hit the same item at the second time.( it does work when clicking a different item at the second time) What is the problem? plz help me
partial code is here
//get task object from menu
taskListArr = new ArrayList<Task>();
taskListArr = getCurrentTasks(taskListArr);
myTask=new TaskListAdapter(this, 0, taskListArr);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, TaskModel.sorts);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sortSpinner.setAdapter(aa);
sortSpinner.setOnItemSelectedListener(this);
#SuppressWarnings("unchecked")
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(arg2 == 0){
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
Collections.sort(taskListArr);
taskListView.setAdapter(myTask);
}
if(arg2 == 1){
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
Collections.sort(taskListArr, new DateComparator());
taskListView.setAdapter(myTask);
}
if(arg2 == 2){
Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
Collections.sort(taskListArr, new PriorityComparator());
taskListView.setAdapter(myTask);
}
position = arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
Check out spinner in android developers site http://developer.android.com/reference/android/widget/Spinner.html
A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.
It selects one child at a time. So Selecting the already selected child again will not invoke onItemSelected function.
i agree with user936414 answer he is right but if still you want that your toast come again then add a on touch listener on your spinner and in the ontouch event add this
line sortSpinner.setOnItemSelectedListener(this);
by this each time as you touch your spinner listener will get invoked again and you will get the on itemselected each time
I added to my class MyActivity the following :
private void updateMyList(){
listing=new ArrayList<listing>();
for(int i =0;i<10;i++)
{
Users user=new Users();
user.setListingName("Name" + i);
user.setListingPhone("i" + i);
listing.add(user);
}
MyListAdapter lfa = new MyListAdapter(this, listing);
((ListView)findViewById(R.id.listFeed)).setAdapter(lfa);
}
This code generates 10 list views so I'd like to add a click listener, so when I click on one of the 10 lists, I get a message or sthing.
Thank you for your help.
I don't see why you couldn't just add an onItemClickListener in your loop to your ListView. In short, use your Adapter to create the list then just attach the listener:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, connections.toArray(new String[connections.size()])));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View item, int position, long id) {
String item = (String) lv.getItemAtPosition(position);
}
});
This is if you want to know which item in each list was clicked, there is an setOnClickListener method as well in case you just want to know whether a ListView has been clicked or not.
Ive been struggling to get an alert dialog to pop up when the user clicks an item in the list view. I can do a toast message but cant seem to get an alert dialog to work. I then want to have a button in the alert dialog to allow me to take the item just selected and display it on the next screen where it will be show contact details etc for the one selected. If anyone can give me any insight into any good techniques or tips on how to do this it would be awesome! Im a very new programmer and ive tried everywhere.
Below is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] taxi = getResources().getStringArray(R.array.taxi_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listtaxi, taxi));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
}
});
}
}
The array is in the strings.xml file.
Any help would be very much appreciated.
Replace
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
with
Toast.makeText(lv.getContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
Your code in general is correct. I'd make sure your code for posting a dialog works correctly first. Try it out in a way outside of the list, and then see if it works in the onClick() statement itself.
Try using YourClass.this instead of getApplicationContext().