Android:一个能够处理40个按钮的监听器

时间:2012-12-11 06:58:01

标签: android sqlite button onclicklistener horizontalscrollview

我的android应用程序中有一个 Horizo​​ntalScrollView ,在其中我有 40按钮,我想实现一个侦听器,它根据点击的按钮从数据库中检索数据例如:

  

如果单击按钮编号1 ,则侦听器应检索   数据库中与第1周

相关的数据

我是否必须为每个按钮实现一个监听器,或者是否有任何方法可以知道单击了哪个按钮并使用一个监听器来更改按钮号和周检索所有按钮?

数据库查询方法

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

这句话应该是动态的,具体取决于点击的按钮

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

如果您使用Activity工具onClickListener,则可以区分单击的按钮。这种方法的一个例子:

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

您可以通过将标签设置到按钮然后使用以下内容来实现更好:

int numberOfWeek=v.getTag();