Android OnClickListener 未在 GridView 中触发(仅限 2.2)
我有一个由 CursorAdapter 生成的按钮网格视图。当 CursorAdapter 传递到 Gridview 时,视图会正确呈现,但网格中的第一个项目不会触发 OnClickListener 事件。
如果我在网格中选择另一个按钮,该事件会正确触发,但是如果我选择第一个按钮然后另一个按钮,它会加载第一个按钮操作,然后加载部分按钮操作。
测试时,这似乎只是我的模拟器上的 Android 2.2 中的问题,我的 1.5 手机按预期工作。我已经擦除了模拟器,但这似乎没有什么区别。
public class AdapterMedia extends CursorAdapter {
Context context;
Cursor cursor;
public AdapterMedia(Context context, Cursor dataset)
{
super(context, dataset);
this.context = context;
this.cursor = dataset;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2)
{
Button imageView;
imageView = new Button(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setPadding(8, 8, 8, 8);
imageView.setId(cursor.getInt(0));
imageView.setText(cursor.getString(1));
imageView.setOnClickListener(buttonClickListener);
return imageView;
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2)
{
Button imageView = (Button) arg0;
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setPadding(8, 8, 8, 8);
imageView.setId(cursor.getInt(0));
imageView.setText(cursor.getString(1));
imageView.setOnClickListener(buttonClickListener);
}
public OnClickListener buttonClickListener = new OnClickListener()
{
public void onClick(View view)
{
Bundle dataset = new Bundle();
dataset.putInt("media_id", view.getId());
Intent showMedia = new Intent(context.getApplicationContext(), MediaActivity.class);
showMedia.putExtras(dataset);
context.startActivity(showMedia);
}
};
}
I have a grid view of buttons that is generated by a CursorAdapter. When the CursorAdapter is passed to the Gridview the view renders correctly however the first item in the grid does not fire the OnClickListener event.
If I select another button in the grid, the event fires correctly however if I selected the first button then another button, it loads the first button action then the section button action.
When testing this, it only seems to be an issue in Android 2.2 on my emulator, my 1.5 phone works as expected. I've wiped the emulator but that doesn't seem to have made a difference.
public class AdapterMedia extends CursorAdapter {
Context context;
Cursor cursor;
public AdapterMedia(Context context, Cursor dataset)
{
super(context, dataset);
this.context = context;
this.cursor = dataset;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2)
{
Button imageView;
imageView = new Button(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setPadding(8, 8, 8, 8);
imageView.setId(cursor.getInt(0));
imageView.setText(cursor.getString(1));
imageView.setOnClickListener(buttonClickListener);
return imageView;
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2)
{
Button imageView = (Button) arg0;
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setPadding(8, 8, 8, 8);
imageView.setId(cursor.getInt(0));
imageView.setText(cursor.getString(1));
imageView.setOnClickListener(buttonClickListener);
}
public OnClickListener buttonClickListener = new OnClickListener()
{
public void onClick(View view)
{
Bundle dataset = new Bundle();
dataset.putInt("media_id", view.getId());
Intent showMedia = new Intent(context.getApplicationContext(), MediaActivity.class);
showMedia.putExtras(dataset);
context.startActivity(showMedia);
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
GridView
对象上设置onItemClickListener
,而不是在每个图像上设置onClickListener
。Set
onItemClickListener
on theGridView
object instead of setting anonClickListener
on each image.