从列表中获取选定的项目 [英] Get Selected Item from List

查看:84
本文介绍了从列表中获取选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个库存系统,用户将在该系统中从列表中选择一个项目,并且右侧的图标将根据用户选择的项目进行更新.

I'm currently creating an inventory system, in which the user will select an item from a list and the icon on the right will update based on the item that the user has picked.

我需要一种获取用户当前选择的列表项的方法.然后,我需要使用该列表项来显示用户可以看到的图标.

I need a way to get the list item that the user has currently selected. I then need to use that list item to display an icon which the user will see.

当前,我尝试使用清单列表上的getSelected()方法,该方法似乎只返回列表中的第一项.我需要一种获取用户当前选择的项目的方法.

Currently I have tried using the getSelected() method on the inventory list, which seems to be only returning the first item in the list. I need a way to get the item that the user has currently selected.

我需要在列表中选择库存"中的当前项目.

I need to get the current item selected on the list called 'inventory'.

package com.sps.game.inventory;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;

import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.sps.game.controller.InventoryController;
import com.sps.game.controller.PlayerController;

public class PlayerInventory {
    public Stage stage;
    public SpriteBatch sb;
    private Viewport viewport;

    private Skin skin = new Skin(Gdx.files.internal("core/assets/pixthulhuui/pixthulhu-ui.json"));

    private List<Item> inventory;
    private List<Image> itemImages;

    private InventoryController inventoryController;
    private InputProcessor oldInput;


    public PlayerInventory(SpriteBatch sb, PlayerController playerController) {
        this.sb = sb;
        viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera());
        stage = new Stage(viewport, sb);

        inventoryController = new InventoryController();
        inventory = inventoryController.getInventoryList();
        itemImages = inventoryController.getImageList();
    }

    private void formatting() {
        stage = new Stage();
        Label inventoryLabel = new Label("Inventory", skin);
        Label imageLabel = new Label("Item", skin);

        Table table = new Table(skin);
        table.setDebug(true);
        table.defaults();
        table.center();
        table.setFillParent(true);
        table.add(inventoryLabel);
        table.add(imageLabel);
        table.row();


        table.add(inventory); //need a way to get the current item selected 
        table.add(itemImages.getSelected()); 

        stage.addActor(itemImages);
        stage.addActor(table);
    }
    public void setInput() {
        oldInput = Gdx.input.getInputProcessor(); //Get the old input from the user.
        Gdx.input.setInputProcessor(stage);       //Set the input to now work on the inventory.
    }

    public void update() {
        if (Gdx.input.isKeyPressed(Input.Keys.I) && oldInput == null) {
            formatting();
            setInput();
        }

        if (Gdx.input.isKeyPressed(Input.Keys.O) && oldInput != null) {
            stage.dispose();
            Gdx.input.setInputProcessor(oldInput);
            oldInput = null;
        }
    }

    public void dispose() {
        stage.dispose();
    }

}

推荐答案

我已经通过使用Clicklistener找到了解决方案.

I have found the solution, through using a Clicklistener.

    inventory.addListener(new ClickListener() {
        public void clicked(InputEvent event, float x, float y) {
            String clickedItem = inventory.getSelected();
            table.add(clickedItem);
            System.out.println(item.getName());
            }
        });

这篇关于从列表中获取选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆