Android OnClickListener 未在 GridView 中触发(仅限 2.2)

发布于 2024-09-13 12:43:06 字数 1830 浏览 7 评论 0原文

我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-09-20 12:43:08

GridView 对象上设置 onItemClickListener,而不是在每个图像上设置 onClickListener

Set onItemClickListener on the GridView object instead of setting an onClickListener on each image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文