Accès Adaptateur Vue de dialogue
-
09-10-2019 - |
Question
J'ai mon ListActivity que lorsque vous tapez sur un élément, il affiche une boîte de dialogue qui demande à l'utilisateur pour l'utilisateur et le mot de passe. Comment puis-je obtenir la position choisie dans la boîte de dialogue?
Voici comment j'initialiser le ListActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
showDialog(DIALOG_USER_PASSWORD);
}
});
}
La boîte de dialogue I pop up est un simple AlertDialog avec 2 EditText que je gonfle à partir d'un fichier xml
protected Dialog onCreateDialog(int id) {
switch (id) {
...
case DIALOG_USER_PASSWORD:
LayoutInflater factory = LayoutInflater.from(this);
final View dialogView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(MyListActivity.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.ask_user_password)
.setView(dialogView)
.setPositiveButton(R.string.ok_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String userName = ((EditText) findViewById(R.id.username_edit_alert_dialog))
.getText().toString();
String password = ((EditText) findViewById(R.id.password_edit_alert_dialog))
.getText().toString();
Credentials cred = new CredentialsL1(userName, password);
/* HERE IS WHERE i NEED THE SELECTED ITEM
mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
mService.connect(mId, cred);
}
})
// Cancel button
.setNegativeButton(R.string.cancel_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.create();
}
return null;
}
Le seul que je suis venu avec est la création d'un nouveau champ « mId » et la mise lorsque les robinets de l'utilisateur et l'utiliser lorsque tapote l'utilisateur sur OK dans la boîte de dialogue. Toute idée plus élégante? Merci
La solution
private int selectedPosition;
...
protected void onCreate(Bundle savedInstanceState) {
....
// inside the item listener...
selectedPosition = position;
showDialog(DIALOG_USER_PASSWORD);
/* HERE IS WHERE i NEED THE SELECTED ITEM
mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
// just use selectedPosition var
Une idée plus élégante?
Il semble que vous utilisez un ListView normal (pas une case à cocher d'un) ... donc, il va bien pour le faire de cette façon.