Question

I am having an issue while trying to use a GridView in a PopupWindow. On my Activity's onCreate method, I am inflating a gridview from xml as follows:

LayoutInflater inflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
final GridView popupview = (GridView) inflater.inflate
(R.layout.gridviewpopup, null, false);
popupview.setAdapter(new ImageAdapter(this));

would like this GridView to popup on a button click. Also inside of my activity's onCreate, I have:

final Button addButton = (Button) findViewById(R.id.add);
 addButton.setOnClickListener(new View.OnClickListener() {  
 public void onClick(View v) {  
     PopupWindow mwindow = new PopupWindow(popupview, 100, 100);
     mwindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 100, 100);
     mwindow.setFocusable(true);  
            }  
 }); 

On button click, I am throwing a ClassCastException from GridView.onMeasure(int, int).

Can anyone please explain to me what I am doing wrong?

Was it helpful?

Solution

I ended up finding the problem. I was using the ImageAdapter code form the Hello, Gallery example. That contained a line of code referencing a Gallary:

imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));

When used with a GridView, this obviously causes a class cast exception.

Now that I have my GridView displaying properly in a popupwindow, I am having trouble capturing the OnItemClick event. Below is my code. OnItemClick is never being called when I make a selection in my gridview in the popupwindow. Any ideas?

final GridView gView = (GridView) grid_layout.findViewById
(R.id.gridview_layout);
gView.setWillNotDraw(false);
gView.setFocusableInTouchMode(true);
gView.setClickable(true);
gView.setAdapter(new ImageAdapter(this));

final PopupWindow soundSelectorWindow = new PopupWindow(this);
soundSelectorWindow.setContentView(grid_layout);
soundSelectorWindow.setBackgroundDrawable(new BitmapDrawable());
soundSelectorWindow.setOutsideTouchable(false);
soundSelectorWindow.setTouchable(true);

gView.setOnItemClickListener(new OnItemClickListener()
    {
     public void onItemClick(AdapterView parent, View v, int position,
long id)
      {
       //Never gets here.
       soundSelectorWindow.dismiss();
      }
 }); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top