将按钮加载到ListView - Android Studio中

时间:2015-05-12 21:41:04

标签: android android-layout android-studio

我正在创建一个动态菜单,该菜单从文件中读取并在该文件中创建整个按钮。我可以使用simple_list_item_1加载列表,您将在下面的代码中看到,但它调用.toString方法,因此列表被内存引用的文本和不是按钮的文本占用,它不是实际的按钮。我已经完成了阅读,并且不知道我需要做什么,因为我读到的东西比我需要的要多得多。虽然代码是从Widget库中制作普通按钮,但最终游戏是使用ImageButton。我还没有制作图像所以按钮是一个调试占位符。 (对不起,但是这一点不必要的信息,只是说有人有更好的想法,然后制作一个按钮列表。)

谢谢!

这是我的代码:

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

    }
}

1 个答案:

答案 0 :(得分:1)

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

gcloud compute instances decribe instance_name

Then you will replace this code:

public class GameItem{
    private String gameName;

   //getters and setters

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

with:

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

And this:

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

with:

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

And then you should implement the OnItemSelectedListener:

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

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

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

            }
        });

Your code will look like this:

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