2010-12-14 3 views
0

私はListActivityを持っています。アイテムをタップすると、ユーザーにユーザーとパスワードを要求するダイアログがポップアップします。ダイアログから選択した位置を取得するにはどうすればよいですか?ここでダイアログボックスからAcess AdapterView

は私が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); 
    } 
}); 
} 

ダイアログは、私は、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; 
    } 

のみを膨らま2のEditTextとシンプルAlertDialogである私が来ています新しいフィールド "mId"が作成され、ユーザーがタップしたときにこのフィールドを設定し、ユーザーがダイアログでOKをタップしたときにそのフィールドを使用します。それ以上のエレガントなアイデア? おかげ

答えて

1
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 

どれよりエレガントなアイデア?

あなたがそう、それはこのようにそれを行うにはいいのよ...通常のリストビュー(ないチェックボックス1)を使用しているようです。

関連する問題