dimanche 13 décembre 2015

Android Identifying Multiple Button

I have morethan 100 button in my MainLayout.xml and i want to handle click on each. I want to group all the button and then listen to click of the group button, get the ID and put it into String and then use it as my base id to query from my Sqlite Databse Table. How can i do this? so far i have this on my MainClass.java.

MainClass.java

public class MainClass extends AppCompatActivity implements View.OnClickListener {
    Button B0,B1,B2,B3....;
    String baseId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        buttonInitializer();
    }

     public void buttonInitializer(){
        B0 = (Button)findViewById(R.id.B0); B0.setOnClickListener(this);
        B1 = (Button)findViewById(R.id.B1); B1.setOnClickListener(this);
        ...
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.B0: 
                baseId = "B0";
                break;
            case R.id.B1: 
                baseId = "B1";
                break;
            ..........
            default:
                break;
        }
        QueryFire(); // Fire a query
    }

   Public void QueryFire(){
      //Fire the query with id of the clicked button
   }
}

As you can see if i continue doing this, i will have Long Code, i want to shorten my code, instead of the above code, i'd like to group the button and then listen of each click just like RadioButton, but NOTE. i dont want to use RadioButton. I'm trying to implement this seudo code.

// Put the button into RelativeLayout or something and use it to group the button
// OnClick of Each Button inside the GroupButton:
   // Get the ID of the Clicked Button:
   // Put the ID of Button into String:
   // and FIRE my query with ID of Button

Any help woul be appreciated! Thank You Very Much!!!

Aucun commentaire:

Enregistrer un commentaire