Android的OnClickListener不是在GridView的射击(2.2只) [英] Android OnClickListener not firing in GridView (2.2 only)

查看:134
本文介绍了Android的OnClickListener不是在GridView的射击(2.2只)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由CursorAdapter的生成按钮,网格视图。当CursorAdapter的被传递到GridView的观点正确呈现然而,在网格中的第一项不火的OnClickListener事件。

如果我选择在网格中的另一个按钮,事件触发正确但是如果我选择的第一个按钮,然后另一个按钮,它加载的第一个按钮动作则部分按钮操作。

在此测试,它只是似乎是在Android 2.2的对我的模拟器的问题,如预期1.5我的手机作品。我抹了模拟器,但似乎并没有做出区别。

 公共类AdapterMedia扩展的CursorAdapter {    上下文语境;
    光标光标;    公共AdapterMedia(上下文的背景下,光标数据集)
    {
        超(背景下,数据集);
        this.context =背景;
        this.cursor =数据集;
    }    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup ARG2)
    {
        按钮ImageView的;        ImageView的=新按钮(背景);
        imageView.setLayoutParams(新GridView.LayoutParams(85,85));
        imageView.setPadding(8,8,8,8);
        imageView.setId(cursor.getInt(0));
        imageView.setText(cursor.getString(1));
        imageView.setOnClickListener(buttonClickListener);        返回ImageView的;
    }    @覆盖
    公共无效bindView(查看为arg0,ARG1背景下,光标ARG2)
    {
        按钮的ImageView =(按钮)为arg0;        imageView.setLayoutParams(新GridView.LayoutParams(85,85));
        imageView.setPadding(8,8,8,8);
        imageView.setId(cursor.getInt(0));
        imageView.setText(cursor.getString(1));
        imageView.setOnClickListener(buttonClickListener);
    }    公共OnClickListener buttonClickListener =新OnClickListener()
    {
        公共无效的onClick(查看视图)
        {
            捆绑数据=新包();
            dataset.putInt(media_id,view.getId());            意图showMedia =新意图(context.getApplicationContext(),MediaActivity.class);
            showMedia.putExtras(数据);            context.startActivity(showMedia);
        }
    };
}


解决方案

设置 onItemClickListener GridView控件对象上,而不是设置的 onClickListener 每幅图像。

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);
        }
    };
}

解决方案

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

这篇关于Android的OnClickListener不是在GridView的射击(2.2只)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆