Android:onItemClick仅返回第一个选定的项目

发布于 2024-09-29 05:53:58 字数 1566 浏览 7 评论 0原文

我正在使用 ArrayAdapter 来填充 ListView。选择项目后,会显示确认是/否对话框。如果用户的选择是否定的,那么他应该能够选择显示相同对话框的另一个项目。等等。

这是我的代码:

lView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {


       Toast.makeText("Selected file"+ mFiles.get(index).getfileName(),
       Toast.LENGTH_SHORT).show();


       SelectedFile = mFiles.get(index);



          showDialog(DIALOG_CONFIRMIMPORT_ID);
       }
    });

奇怪的是,虽然“Toast”每次都显示单击的项目,但只有自活动启动以来第一个选定的项目被传递给“SelectedFile”。无论您单击不同的项目多少次,“SelectedFile”始终假定相同的值,即在此代码之外的第一次单击的项目的值。

这是我的对话框代码:

Protected Dialog onCreateDialog(int id) {
switch(id) {
 case DIALOG_CONFIRMIMPORT_ID:
  {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  String message = String.format(getString(R.string.importstudentfileconfirm),SelectedFile.getfileName());

  builder.setMessage(message)
  .setCancelable(false)
  .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
  //     Activity.this.finish();
  //     startActivity(new Intent(Activity.this, LOL.class));
  }
  })
 .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
 SelectedFile = null;
 dismissDialog(DIALOG_CONFIRMIMPORT_ID);
 mFiles.notifyAll();
 }
 });

 AlertDialog alert = builder.create();
 return alert;
 }
 }
  return null;
 }

非常感谢您的帮助!

I'm using and ArrayAdapter to populate a ListView. After selecting and item, it displays a confirmation Y/N dialog. If the user's choice is negative, then he should be able to select another item showing the same dialog. And so on.

Here's my code:

lView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {


       Toast.makeText("Selected file"+ mFiles.get(index).getfileName(),
       Toast.LENGTH_SHORT).show();


       SelectedFile = mFiles.get(index);



          showDialog(DIALOG_CONFIRMIMPORT_ID);
       }
    });

The weird thing is that while the "Toast" shows the clicked item every time, only the first selected item since the Activity is initiated is being passed to "SelectedFile". No matter how many times you click a diferent item, "SelectedFile" always assumes the same value, the value of the first clicked item, outside of this code.

Heres's my Dialog code:

Protected Dialog onCreateDialog(int id) {
switch(id) {
 case DIALOG_CONFIRMIMPORT_ID:
  {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  String message = String.format(getString(R.string.importstudentfileconfirm),SelectedFile.getfileName());

  builder.setMessage(message)
  .setCancelable(false)
  .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
  //     Activity.this.finish();
  //     startActivity(new Intent(Activity.this, LOL.class));
  }
  })
 .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
 SelectedFile = null;
 dismissDialog(DIALOG_CONFIRMIMPORT_ID);
 mFiles.notifyAll();
 }
 });

 AlertDialog alert = builder.create();
 return alert;
 }
 }
  return null;
 }

Thank you very much for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-10-06 05:53:58

我猜这与 onCreateDialog 方法仅在第一次创建对话框时被调用有关。因此,当您第一次看到该对话框时,它将具有正确的文件名。

调用 onCreateDialog 后,调用 onPrepareDialog(...)。 onPrepareDialog,允许您在对话框创建之后、显示之前更改对话框。

请记住,在所有内容之下,每次您想要显示 DIALOG_CONFIRMIMPORT_ID 对话框时,Android 都不会为您创建一个新对话框。每次实例化一个新对话框的计算成本太高。相反,它创建一次,这会导致调用 onCreatDialog,然后调用 onPrepareDialog。每隔一次显示对话框时,它只调用 onPrepareDialog。

请查看 Android 开发者网站上的以下文章。它解释得很清楚。

http://developer.android.com/guide/topics/ui/dialogs .html#ShowingADialog

因此,尝试使用 onCreateDialog 来初始化在对话框显示之间不会改变的内容,然后使用 onPrepareDialog 方法动态更新对话框的内容(即获取新文件名)

干杯!

I'm guessing this has something to do with the fact that the onCreateDialog method is only called the first time the dialog is created. So the first time you see the dialog it will have the correct filename.

After onCreateDialog is called, onPrepareDialog(...) is called. onPrepareDialog, allows you to change the dialog after it has been created, but before it gets displayed.

Remember that underneath everything, Android isn't creating a new Dialog for you every time you want to show the DIALOG_CONFIRMIMPORT_ID dialog. It is too computationally expensive to instantiate a new dialog every time. Instead, it creates it once, which causes onCreatDialog to be called, followed by the onPrepareDialog. Every other time the dialog is shown, it only calls onPrepareDialog.

Check out the following article on the Android Developer site. It explains things pretty clearly.

http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

So try using onCreateDialog just for initialization of stuff that won't change between showings of the dialog, then use the onPrepareDialog method to dynamically update the contents of the dialog (i.e. getting the new filename)

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文