Search code examples
androidsqlitebuttononclicklistenerhorizontalscrollview

Android: a listener to handle 40 button with differences


I have a HorizontalScrollView in my android application and inside it I have 40 button, I want to implement a listener which retrieve data from database depending on the button clicked for example:

if the button number 1 was clicked, the listener should retrieve data from database which related to week 1

do I have to implement a listener to each button or is there any way to know which button was clicked and use one listener to all the buttons with a change of the button number and week to retrieve ?

Database query method

DataBaseHelper myDbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = myDbHelper.getReadableDatabase();

        String selectWebQuery = "select * from zweeks where zweeks_id = " + weekSince ;
        Cursor cursor = db.rawQuery(selectWebQuery, null);

        int CurrentDay=(int) (numberOfDaysSince%7);
        if(CurrentDay==0)CurrentDay=7;
        System.out.println("current Day="+CurrentDay);

        String     string1 =null;
        String     string2 = null;
        String     string3 = null;
        String     string4 = null;
        String     string5 = null;
        String     string6 = null;
        String     string7 = null;
        String tags=null;

        if (cursor != null)
                { cursor.moveToFirst();
                string1 = cursor.getString(1);
                string2 = cursor.getString(2);
                string3 = cursor.getString(3);
                string4 = cursor.getString(4);
                string5 = cursor.getString(5);
                string6 = cursor.getString(6);
                string7 = cursor.getString(7);

          String tags1 ="<head><style>body{float:right;text-align:right;word-wrap: break-word; }</style><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>";

                switch(CurrentDay){

              case 1:tags=tags1+string1+"<img src=\"file:///android_res/drawable/dottedline.png\" />"+string2+"<br><br>"+string3+"<br><br>"+string4+"<br><br>"+string5+"<br><br>"+string6+"<br><br>"+string7+"<img src=\"file:///android_res/drawable/line.png\" /><br>" ;

              break;
              case 2:tags=tags1+string2+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string3+"<br><br>"+string4+"<br><br>"+string5+"<br><br>"+string6+"<br><br>"+string7+"<br><img src=\"file:///android_res/drawable/line.png\" /><br>"+string1+"<br>" ;

              break;
              case 3:tags=tags1+string3+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string4+"<br><br>"+string5+"<br><br>"+string6+"<br><br>"+string7+"<br><img src=\"file:///android_res/drawable/line.png\" /><br>"+string1+"<br><br>"+string2+"<br>" ;

              break;
              case 4:tags=tags1+string4+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string5+"<br><br>"+string6+"<br><br>"+string7+"<br><img src=\"file:///android_res/drawable/line.png\" /><br>"+string1+"<br><br>"+string2+"<br><br>"+string3+"<br><br>" ;

              break;
              case 5:tags=tags1+string5+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string6+"<br><br>"+string7+"<br><img src=\"file:///android_res/drawable/line.png\" /><br>"+string1+"<br><br>"+string2+"<br><br>"+string3+"<br><br>"+string4+"<br><br>" ;

              break;
              case 6:
                  tags=tags1+string6+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string7+"<br><img src=\"file:///android_res/drawable/line.png\" /><br>"+string1+"<br><br>"+string2+"<br><br>"+string3+"<br><br>"+string4+"<br><br>"+string5+"<br><br>" ;

              break;
              case 7:
                  tags=tags1+string7+"<br><img src=\"file:///android_res/drawable/dottedline.png\" /><br>"+string1+"<br><br>"+string2+"<br><br>"+string3+"<br><br>"+string4+"<br><br>"+string5+"<br><br>"+string6+"<br><br>" ;

              break;

            }

          } 

      webView.loadDataWithBaseURL("file:///android_res/drawable/", tags, "text/html", "UTF-8", null);
}

this sentence should be dynamic depending on the button clicked

  • String selectWebQuery = "select * from zweeks where zweeks_id = " + weekSince ;

Thanks in advance.


Solution

  • It is possible if you make your Activity implement onClickListener and then you can distinguish which button was clicked. An example of such approach:

    public class FirstActivity extends Activity implements OnClickListener {.....
    
    @Override
    public void onClick(View v) { // Parameter v stands for the view that was clicked.  
    
        // getId() returns this view's identifier.
        if(v.getId() == R.id.leftButton){
            // setText() sets the string value of the TextView
            changingTextView.setText("You clicked First");
        }else if(v.getId() == R.id.rightButton){
            changingTextView.setText("You clicked Second");
        }
    }
    

    You could make it even better by setting tags to your button and then using something like :

    int numberOfWeek=v.getTag();