如何在活动中获得特定但不断变化的无线电按钮数量

时间:2016-01-29 00:15:51

标签: java android radiobuttonlist

我正在制作一个简单的半基于文本的冒险游戏。

我正在努力获得一个有效的库存切换系统。

我有一个系统,你的(最多14个)项目显示为radiobuttons的文本,但它留下了很多重复的#34;你没有什么" s。

无论如何,我可以在屏幕上显示X单选按钮,其中X是您可以切换到的当前项目数量?

我尝试创建然后向收音机组添加单选按钮(然后简单地删除我原来拥有的旧14个按钮),但我需要一个名为Context的类,因为我是android的新手我不喜欢#39;了解那是什么。

package com.blogspot.darokrithia.dungeonfungeon;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

/**
 * Created by dtabin17 on 1/21/16.
 */
public class ItemSwitcherActivity extends AppCompatActivity{        //Used to switch items in inventory slots
    int itemType = InventoryActivity.itemToSwitch;;    // lets the class know what kind of item to switch
    RadioGroup listOfItems;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_switcher);
        setupItems(itemType);
    }

    public void onBackClick(View v){                //when player clicks back button
        if (v.getId() == R.id.backButton){
            Intent i = new Intent(ItemSwitcherActivity.this, InventoryActivity.class);
            startActivity(i);                       // goes back to inventory screen
        }
    }

    public void  setupItems(int t){                                             //gets the proper Radio buttons
        int[] equippedInventory = RoomActivity.player.getEquipment();           //here until (look bellow)
        int[] inventory = RoomActivity.player.getInventory();                   //
        ArrayList <Equipment> totalInventory = new ArrayList<>();               //Compiled list of inventory and equipped items
        listOfItems = (RadioGroup) findViewById(R.id.inventoryList);            //The radio group displayed
        ArrayList <Equipment> listedInventory = new ArrayList<>();              //Items already listed.
                                                                                //
        for (int i = 0; i < equippedInventory.length; i++){                     //
            int ID = equippedInventory[i];                                      //
            Equipment e = new Equipment(ID);                                    //
            totalInventory.add(e);                                              //
        }                                                                       //
        for (int i = 0; i < inventory.length; i++){                             //
        int ID = inventory[i];                                                  //
            Equipment e = new Equipment(ID);                                    //
        totalInventory.add(e);                                                  //
        }                                                                       //here gets the players equipment into a one nice and easy to use ArrayList

        switch(t){
            case(0):
                ((RadioButton) listOfItems.getChildAt(0)).setText(totalInventory.get(0).getText());
                int r = 1;
                for (int i = 0; i < (totalInventory.size()-1); i++){
                    if((totalInventory.get(i).isHelmet) && !(listedInventory.contains(RoomActivity.player.getEquipment()[0]))){
                        listedInventory.add(totalInventory.get(r));
                        ((RadioButton) listOfItems.getChildAt(i)).setText(totalInventory.get(r).getText());
                        r++;
                    }
                }
                break;
            case(1):
                //do stuff
                break;
            case(2):
                //do stuff
                break;
            case(3):
                //do stuff
                break;
            case(4):
                //do stuff
                break;
            default:
                Intent i = new Intent (ItemSwitcherActivity.this, InventoryActivity.class);
                startActivity(i);
                break;
        }
    }
}

0 个答案:

没有答案