简体   繁体   中英

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);

    }
}

ListView has the "OnItemSelctedListener" which will tell you which item is selected. It`s better if you create en Java Object EX GameItem which will represent an row of the file:

public class GameItem{
    private String gameName;

   //getters and setters

   @Override
   public String toString(){
       return gameName;
   }
}

Then you will replace this code:

ArrayList<Button> btnList = new ArrayList<>();

with:

ArrayList<GameItem> btnList = new ArrayList<>();

And this:

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();
        }

with:

 while(line != null)
        {
            GameItem tempGameItem = new GameItem();
            tempGameItem.setGameName(line);
            btnList.add(tempGameItem);
            line = buff.readLine();
        }

And then you should implement the OnItemSelectedListener:

buttonPanel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                selectedGameMenu.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

And at the end you should set the Adapter to the ListView:

    ListAdapter btnAdp = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1 ,btnList);
    buttonPanel.setAdapter(btnAdp);

Your code will look like this:

ListView buttonPanel = new ListView(this);
buttonPanel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    selectedGameMenu.setVisibility(View.VISIBLE);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
});
ArrayList<Button> btnList = new ArrayList<>();

     try
    {
       //... your code
            while(line != null)
            {
                GameItem tempGameItem = new GameItem();
                tempGameItem.setGameName(line);
                btnList.add(tempGameItem);
                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);
    }

    //... your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM