【发布时间】: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