I am making a simple semi-textbased adventure game.
I am trying to get a working inventory switching system.
I have a system where your (up to 14) items appear as the text of radiobuttons, but it leaves a lot of them as repetitive "You have nothing"s.
Is there anyway that I can make X radio buttons come on screen, where X is the number of items you currently have that you can switch to?
I tried creating and then adding radio buttons to the radio group (and then simply deleting the old 14 buttons I originally had), but I need a class called Context
, and as I am new to android I don't understand what that is.
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;
}
}
}