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:
LayoutInf开发者_如何学编程later 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?
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();
}
});
精彩评论