jeudi 23 juin 2016

How to refer to a randomly generated button's elements

I need to randomly generate a button and then grab elements from it from the activity that the button opens. Specifically the universal ID of a "character" that I have specified in PersistentData. (That whole class works fine, though. The problem is between this activity and the next.) The child activity has multiple activities that can call it. (There are several activities with lists made from different characters from a pool, and each list has these buttons that are shown below, which all point to the same activity that loads information from PersistentData based on the data that its supposed to pull from said button.) The following 1 block of code is in the onCreate method for this activity. (Automatically generated, I just added this in after I called the layouts from the XML file)

for (fID = 0; fID < PersistentData.fName.size(); fID++) {

        if (PersistentData.fName.get(fID) != null) {
            Button[] Btn = new Button[PersistentData.fName.size()];
            Btn[fID].setHeight(200);
            Btn[fID].setWidth(200);
            Btn[fID].setTextSize(40);
            Btn[fID].setText(PersistentData.fName.get(fID));
            Btn[fID].setTag(fID);
            Layout.addView(Btn[fID]);
            Btn[fID].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int btnID = fID;
                    gotoActivity(v);

                }

            });

        } else continue;

    }

Here is gotoActivity(). (In the class, not onCreate)

public void gotoActivity(View view) {
    Intent intent = new Intent(this, TargetActivity.class);
    startActivity(intent);
    intent.putExtra("btnClicked", /*IDK WHAT TO PUT RIGHT HERE*/);
}

I have put several things there, actually. Mostly, they have been various ways of declaring a variable on the creation of the button.

Here's the TargetActivity.class

public class Fighter extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fighter);

    Intent intent = getIntent();
    Bundle bundle =
            intent.getExtras(); //line ***
    **


    TextView viewName = (TextView) findViewById(R.id.fighterName);

    viewName.setText(PersistentData.fName.get(fID));

    }

}

What should I put in the other class that I should get here**? (fID) is the thing that I'm trying to get. I have tried putting and getting fID using the methods as described in the Create an Activity page from Android and it continued to give me a NullPointerException at line *** (I split the line to be precise.)

Any help is appreciated. I would not be surprised if there is a better way to do this, requiring me to scrap all my work and restart. I would prefer not to, though lol. I will be checking this post periodically throughout the day, so if you have any questions or require more detail, just post or message me. Thank you beforehand.




Aucun commentaire:

Enregistrer un commentaire