简体   繁体   English

将按钮加载到ListView中-Android Studio

[英]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. 我可以在下面的代码中看到,使用simple_list_item_1加载列表,但是它调用.toString方法,因此该列表被作为内存引用的文本占用,而不是按钮的内容,不是实际的按钮。 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. 当代码从Widget库中制作普通按钮时,最终的工作是使用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. ListView具有“ OnItemSelctedListener”,它将告诉您选择了哪个项目。 It`s better if you create en Java Object EX GameItem which will represent an row of the file: 最好创建一个Java Object EX GameItem来代表文件的一行:

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: 然后,您应该实现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: 最后,您应该将Adapter设置为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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