我找了一些关于onItemSelected的答案,但我发现他们需要使用onItemClick。
在我的应用程序中,我希望用户选择在gridView上显示的名称,然后单击“输入”,并显示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();
}
}
答案 0 :(得分:0)
删除gridView.isSelected()。这将返回您的查看状态。