未调用OnItemClickListener

时间:2013-07-12 07:18:21

标签: java android events android-gridview

我在gridview中有一个ONItemClickListener。但我的onitemclicklistener没有被调用。 gridview

的项目点击没有任何活动
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        vi = inflater.inflate(R.layout.home, container, false);

        Button startdialog = (Button) vi.findViewById(R.id.btnCreateDialog);
        startdialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startdialog = new Intent(getActivity(),
                        start_dialog.class);
                startActivity(startdialog);
            }
        });

        Button iv = (Button) vi.findViewById(R.id.btnMoreDialog);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu homepopup = new PopupMenu(getActivity(), v);

                MenuInflater inflater = homepopup.getMenuInflater();

                inflater.inflate(R.menu.moredialog, homepopup.getMenu());

                homepopup.show();

            }
        });

        PremiumgridView = (StaggeredGridView) vi
                .findViewById(R.id.premiumstaggeredGridView);


        new Dialogs().execute(urls);

        return vi;
    }

    private class Dialogs extends AsyncTask<String[], Void, String[]> {
        @Override
        protected String[] doInBackground(String[]... params) {

            return params[0];
        }

        protected void onPostExecute(String[] result) {

            int premiummargin = getResources().getDimensionPixelSize(
                    R.dimen.margin);
            PremiumgridView.setItemMargin(premiummargin);
            PremiumgridView.setPadding(premiummargin, 0, premiummargin, 0);

            final StaggeredAdapter premiumadapter = new StaggeredAdapter(
                    vi.getContext(), R.id.photoimageview, result,
                    R.layout.row_staggered_demo);

            PremiumgridView.setAdapter(premiumadapter);

            premiumadapter.notifyDataSetChanged();

            premiumadapter.onClick(vi);
            PremiumgridView.setOnItemClickListener(new OnItemClickListener() {

                @Override
            public void onItemClick(StaggeredGridView parent, View view,
                    int position, long id) {
                     String item = premiumadapter.getItem(position).toString();
                      Toast.makeText(getActivity(), premiumadapter.getItem(position), Toast.LENGTH_SHORT).show();
                  // Toast.makeText(getActivity(), "You have chose: "+ item, Toast.LENGTH_LONG).show();

            }});


        }

        @Override
        protected void onPreExecute() {

        }
    }

有人请吗?

谢谢,

2 个答案:

答案 0 :(得分:1)

通过从xml中删除按钮解决了问题。可点击项目中不能包含其他可点击项目。参考OnItemClickListener Not Triggered on Android GridView

答案 1 :(得分:0)

它会崩溃吗?如果是,请始终向我们提供Stacktrace / Logcat。

据我所知,从UI-Thread以外的任何线程中直接更改界面是不可能的 - 您可以尝试Handler或使用this

编辑:实际上在UI-Thread上调用了OnPostExecute,因此这不是解决此问题的方法。 (见here