【问题标题】:setOnSelectedListener doesn't work on gridViewsetOnSelectedListener 在 gridView 上不起作用
【发布时间】:2015-09-02 13:29:12
【问题描述】:

我寻找了一些关于 onItemSelected 的答案,但我发现他们需要使用 onItemClick。

在我的应用程序中,我希望用户选择他在 gridView 上显示的姓名,然后单击 Enter,然后出现 alertDialog 以输入他的密码。

尝试了一些东西,但它对我不起作用,这是相关代码:

waiterList = new ArrayList<Waiter>();

    waiterList = dataBaseHelper.showWaiters();
    adapter = new MyAdapter(MainActivity.this, waiterList);

    gridView.setAdapter(adapter);
    gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            waiter = waiterList.get(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

以及调用按钮点击的函数:

public void Enter(View view) {

        if (gridView.isSelected()){

            LayoutInflater inflater = getLayoutInflater();
            View dialogLayout = inflater.inflate(R.layout.password_dialog, null);

            AlertDialog.Builder passwordDialog = new AlertDialog.Builder(MainActivity.this);
            passwordDialog.setTitle(getString(R.string.get_id_uniq));
            passwordDialog.setMessage(getString(R.string.enter_id));

            passwordDialog.setView(dialogLayout);

            passwordDialog.setPositiveButton(getString(R.string.next), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    password = input.getText().toString();
                    if (password.equals(waiter.getPass())) {

                        startActivity(new Intent(getApplicationContext(), Activity_Zone.class));


                        Toast.makeText(getApplicationContext(),
                                "match", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Wrong pass", Toast.LENGTH_LONG).show();
                    }
                }

            });

            passwordDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            AlertDialog dialog = passwordDialog.show();
            input = (EditText) dialog.findViewById(R.id.editText);


            }

        else {
            Toast.makeText(getApplicationContext(), "Waiter isnt selected", Toast.LENGTH_LONG).show();
            }
        }

【问题讨论】:

    标签: android android-activity gridview onclick onitemselectedlistener


    【解决方案1】:

    删除 gridView.isSelected()。这将返回您的视图状态。

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多