0

我正在为我正在制作的游戏制作设备屏幕。我让用户单击图像按钮。此按钮会弹出一个从库存数据库填充的 alertDialog。库存数据库具有字段(_id、Desc、Number、EquipSlot)。当用户单击我想要获取_id 的值的项目之一时,我就可以获取数字。从那里我将获取编号并交叉引用包含游戏中所有项目的数据库。这样我就可以弄清楚它附加了哪些统计数据,并更新了我的数据库,该数据库存储了角色信息以及当前佩戴的设备。我不知道如何让这个 _id 完成上述操作。以下是我到目前为止所拥有的。

 @Override
    protected Dialog onCreateDialog(final int id) {

      switch (id) {
      case DIALOG_MELEE_ID:
          buildDialog();
        break;
      case DIALOG_RANGE_ID:
          buildDialog();
        break;
     ...
      default:
          dialog = null;
              }
      return dialog;
    }

  @Override
    protected void onPrepareDialog(final int id, final Dialog dialog) {
      switch (id) {
      case DIALOG_MELEE_ID:
          pullInventoryCursor(1);
        break;
      case DIALOG_RANGE_ID:
          pullInventoryCursor(2);
        break;
     ...
      }
    }

    public void equipSlot1(View v){
        showDialog(DIALOG_MELEE_ID);
    }

    private void buildDialog(){
        int selectedItem = -1; //somehow get your previously selected choice
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select Weapon").setCancelable(true);
        builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //get _id and update dbs as needed.
                dialog.dismiss();


            }
        });

        dialog = builder.create();
    }

    private void pullInventoryCursor(int equipSlot){
        if (slot == 1){
        inventory = mydbhelper.getInventory1(equipSlot);}
        else if (slot == 2){
            // TODO setup database and dbhelper for character slot 2
            inventory = mydbhelper.getInventory1(equipSlot);
        }
        startManagingCursor(inventory);
    }
4

1 回答 1

3

您可以从对话框中拉出列表视图,然后通过列表视图的适配器在给定位置检索项目的 ID

builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                ListView lv = dialog.getListView();
                long itemId = lv.getAdapter().getItemId(which);

                //do whatever you need with the ID in the DB

                dialog.dismiss();


            }
        });

注:显然

long itemId = lv.getItemIdAtPosition(which);

将与

long itemId = lv.getAdapter().getItemId(which);
于 2012-04-20T20:58:56.263 回答