开发者

Reduce code for Button onClick events

My question has three parts.

My Layout consists of 26 Buttons: ButtonA, ButtonB, ButtonC...ButtonZ

The buttons are arranged in order on the screen. When the button is clicked I want to capture the click event and filter a SQLiteDB of words by the first letter that was clicked.

How do I write the minimum amount of code that will capture the click, identify the button's corresponding letter, and return the letters that begin with the selected letter from the SQLite Database?

Below is my code that has not been optimized for code brevity.

//Create your buttons and set their onClickListener to "this"
    Button b1 = (Button) findViewById(R.id.Button04);   
    b1.setOnClickListener(this);

    Button b2 = (Button) findViewById(R.id.Button03);   
    b2.setOnClickListener(this);


     //implement the onClick method here
public void onClick(View v) {
   // Perform action on click
  switch(v.getId()) {
    case R.id.Button04:
        //Toast.makeText(this,"test a",Toast.LENGTH_LONG).show();

     // Do the Sql开发者_JS百科Select and Listview statement for words that begin with "A" or "a" here.

       break;
    case R.id.Button03:
        //Toast.makeText(this,"test b",Toast.LENGTH_LONG).show();

 // Do the SqlSelect and Listview statement for words that begin with "A" or "a" here.
      break;
  }

}


If you have lots of characters and a button for each character, I would probably extend the "BaseAdapter" class and make a ButtonAdapter holding the alphabet as a character array, instead of actually making 28:ish buttons... ...if I understood the problem correctly?

The getView could look something like this:

  public View getView(int position, View convertView, ViewGroup parent) {
    Button button;

    if (convertView == null) {
        button = new Button(mContext);
        button.setTag(alphabet[position]);
        button.setOnClickListener(clickListener);
    } else {
        button = (Button) convertView;
    }

    button.setText(alphabet[position]);
    return button;
  }


If your layout is defined in XML, you may want to use the android:onClick parameter for your buttons. This way you can save the findViewById() and setOnClickListener() calls.


As alextsc says, you can bind the click listeners from XML if you wish you reduce code in your Activity. However, I think it might be nicer to define the buttons in code inside a loop given their nature. Just make a simple container in your layout (e.g. a LinearLayout) and add to buttons to that. We can exploit the fact that you can loop over characters (treating them like integers) like so:

for (char letter = 'a'; letter <= 'z'; letter++) {
    Button button = new Button(this);
    button.setText(String.valueOf(letter));
    button.setOnClickListener(this);
    container.addView(button);
}

As for your actual click listener, you don't really need to switch on their IDs to do what you want. You know that the data for the button is just their text value. Just extract that and use it in a uniform way:

public void onClick(View v) {
        Button button = (Button) v;
        String letter = button.getText().toString();
        // filter using letter
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜