0

我的 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 ;

提前致谢。

4

1 回答 1

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();
于 2012-12-11T07:02:59.933 回答