Cannot execute OnItemClick for GridView in PopupWindow
I have a gridView that I display in a popupwindow (the gridview is in a transparent layout, which inherits from linearlayout and just has a partially transparent background). I can never get this GridView's OnItemClick to execute. When I touch an image in my gridview it appears to be clicked (image bachgrond changes), but OnItemClick is not being called.
Below is the code for my Adapter and my popupview containing the gridView. Thanks!
//Adapter
public class ImageAdapter extends BaseAdapter { private Context mContext; private int itemBackground;
public ImageAdapter(Context c) {
mContext = c;
//---setting the style---
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
....
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setBackgroundResource(itemBackground);
return imageView;
}
public Integer[] images = {
R.drawable.sound1,
R.drawable.sound2,
R.drawable.sound3,
R.drawable.sound4
};
}
//////////In Activity, onCreate////////
...
final LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final TransparentLayout musicGrid = (TransparentLayout) inflater.inflate(R.layout.gridviewpopup, null, false);
final GridView gView = (GridView) musicGrid.findViewById(R.id.music_gridview);
gView.setAdapter(new ImageAdapter(this));
final PopupWindow soundSelectorWindow = new PopupWindow(this);
soundSelectorWindow.setContentView(musicGrid);
soundSelectorWindow.setTouchable(true);
gView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//NEVER GETS HERE
soundSelectorWindow.dismiss();
}
});
Asked by: Audrey681 | Posted: 20-01-2022
Answer 1
what happens if you remove soundSelectorWindow.setTouchable(true);
Answer 2
I can't explain why the OnItemClickListener isn't working, but when I substituted with OnTouchListener it worked. Do you need to differentiate between which item is clicked or is it enough just to know that the popup was clicked? From you code it looks like the latter, so OnTouch should work:
popup.setTouchable(true);
popup.setFocusable(true);
gView.setClickable(true);
gView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
popup.dismiss();
return true;
}
});
Answered by: Elise460 | Posted: 21-02-2022
Answer 3
Check the VM size of the Emulator so that its good enough to run your app in its VM.
Check the Size of your <ApplicationName.apk>
. Keep it in min sized so that Emulator will not find any difficulties in executing your app.
Similar questions
How do I refresh a ListView in Android - onItemClick, Alert shown with Edit Text
I have a a list view that shows:
tag : 10
tagb : 20
When I click on an item, I get an Alert asking to change a value (EditText)
I set the value and click ok
How can I refresh the listview to show the change
How can I manually change the row and put the new value there eg tagb : 122?
BTW, The list of itemsand value comes from a result of a REST call I make to a server
Android onItemClick -> Toast bookmark id
Been trying to find out what is the bookmark id the user clicked on...
Tried everything, many force closes... and now an empty toast (no error marks in eclipse):
public class Dmarks extends ListActivity {
protected Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextVi...
listview - Android: onItemClick only returns first selected Item
I'm using and ArrayAdapter to populate a ListView. After selecting and item, it displays a confirmation Y/N dialog. If the user's choice is negative, then he should be able to select another item showing the same dialog. And so on.
Here's my code:
lView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View v, final int index, f...
Android-ListView- access local variables in onitemclick method
I am building an android application which has a listview and when the user clicks on the listview item then a new activity is started. I want to pass some data to the new activity. I have two local variables titles and descriptions which I want to use inside that method. Compiler is throwing an error saying that descriptions should be a final type to use in there. Because the descriptions array is generated dynamically I...
android - Displaying Cursor Results from DB in ListView after OnItemClick
I have an activity where I want it to display results from a database using ListView. The table has three columns: word, description, and category. On the activity page, I have a list of categories being read from an array. I want to set it up so that if you click on an item on the list (for example Cat1), the results returned from the cursor will be all words/descriptions with the category Cat1 in the DB. Currently I ...
android - dialog has not dismiss() in onitemclick listener
AlertDialog.Builder dialog = new AlertDialog.Builder(this)
has not (or not showing) dismiss() method under setOnItemClickListener() ??
particularly this is my code.
AlertDialog.Builder dialog = new AlertDialog.Builder(getApplicationContext());
dialog.setTitle("TITLE");
dialog.setView(view);
dialog.show();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onI...
android onItemClick silently fails
I'm displaying some sql data in a list via a ListAdapter. Everything works fine except when I try to set a click listener to each item in the list. Nothing happens when I click any of the items; no error messages, it just silently fails.
public class Notes extends ListActivity implements OnClickListener {
private static final String TAG = "Notes";
private NotesData notes;
public void onCreate(B...
android How to get image path from GridView through onItemClick listener
Got stuck a bit since i cannot understand how to get the path from
a picture i click in the GridView.
This listener is the problem since i load the GridView with images from a folder on the sdcard.
public void onItemClick(AdapterView parent, View v, int position, long id)
I can only see example on how to use the "position" when the GridView is loaded from resources.
Can an...
Android ListView OnItemClick Change View Properties
What I have: A custom listview with a custom adapter, with a custom ListViewItem (which extends LinearLayout)
What i want to do: When the listbox item is selected, I want to change the background drawable of the selected view.
What Happens: Currently nothing. The Toast does appear as it should, but the Background drawable does not change. I tried calling V...
java - How to get position of item selecte in gridview, or detect release of key with onItemClick?
I must be able to detect the press and release of a button, but I must also have the position of the item in the gridview that is being touched. I basically have a gridview that is a d-pad control. So when the user pushed on it i need it to send "go" and when they release i need it to send "stop"
with this below , there is no way to tell when they lift off the button (that i know!).
Grid.setAdap...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android