mardi 12 mai 2015

Loading Buttons into a ListView - Android Studio

I am creating a Dynamic menu that reads from a file and creates a button for entire in that file. I can load the list with simple_list_item_1 as you will see in the code bellow, but it calls the .toString method so the list is occupied by text that is memory references and what not of the button, It's not the actual button. I've done reading and don't know exactly what I need to do because the stuff I read is so much more than I need. While the code is making plain buttons from the Widget Library, the end game is to use the ImageButton. I haven't made the images yet so the button is a Debugging placeholder. (Sorry but this little bit of unneeded info, just saying in-case someone has a better idea then making a list of buttons.)

Thanks!

Here is my code:

public class GameSelect extends Activity

{ private IDGen makeID = new IDGen();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //This is the main layout of the Activity
    RelativeLayout gameSelectMenu = new RelativeLayout(this);
    //Rules for the Title
    RelativeLayout.LayoutParams titleZone = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    titleZone.addRule(RelativeLayout.CENTER_HORIZONTAL);
    titleZone.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    //This is the Title
    ImageView titleImage = new ImageView(this);
    titleImage.setBackgroundResource(R.drawable.gameselect_gameselecttitle);
    gameSelectMenu.addView(titleImage, titleZone);

    //Rules for the Button Panel
    RelativeLayout.LayoutParams buttonPanelPram = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    buttonPanelPram.addRule(RelativeLayout.CENTER_VERTICAL);
    buttonPanelPram.addRule(RelativeLayout.CENTER_HORIZONTAL);

    //This is the sub-parent
    final RelativeLayout selectedGameMenu = new RelativeLayout(this);
    //selectedGameMenu.setId(idParent);
    selectedGameMenu.setBackgroundColor(Color.GRAY);

    //TODO The prams for this needs to be better.
    //This is the LinearLayout
    ListView buttonPanel = new ListView(this);
    ArrayList<Button> btnList = new ArrayList<>();

    try
    {
        //This is used to declare the AssetManager
        AssetManager content = getAssets();
        //We load the file into the Stream
        InputStream input = content.open("PlayerDataSheet.txt");
        //Buffer the Stream here
        BufferedReader buff = new BufferedReader(new InputStreamReader(input));
        //Load the first line of stream into this var
        String line = buff.readLine();
        //Starts the Dynamic button process
        while(line != null)
        {
            Button gameBtn = new Button(this);
            gameBtn.setText(line);
            gameBtn.setId(makeID.newID());
            gameBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedGameMenu.setVisibility(View.VISIBLE);
                }
            });
            btnList.add(gameBtn);
            line = buff.readLine();
        }
        //Closing all streams that were opened.  May have been the reason for frame loss
        buff.close();
        input.close();

        ListAdapter btnAdp = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1 ,btnList);
        buttonPanel.setAdapter(btnAdp);
    }
    catch(FileNotFoundException e)
    {
        Log.e("File", "File for button data was not found");
    }
    catch(IOException e)
    {
        Log.e("File", "File was found, input stream is empty");
    }

    //Adds the newly filled button panel to the main parent
    gameSelectMenu.addView(buttonPanel, buttonPanelPram);
    buttonPanel.setVisibility(View.VISIBLE);

    //This is just a testing line to occupy the sub-parent until I have what I need
    TextView testingOne = new TextView(this);
    testingOne.setText("If you see this it worked!");
    selectedGameMenu.addView(testingOne);
    selectedGameMenu.setVisibility(View.INVISIBLE);

    //Rules for the SubParent
    RelativeLayout.LayoutParams subParent = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    subParent.addRule(RelativeLayout.CENTER_VERTICAL);
    subParent.addRule(RelativeLayout.CENTER_HORIZONTAL);

    //Adds the sub-parent to the main parent
    gameSelectMenu.addView(selectedGameMenu, subParent);

    setContentView(gameSelectMenu);

}}

Aucun commentaire:

Enregistrer un commentaire