为每个循环按钮提供Id,并根据按钮的id设置单击监听器

时间:2014-11-12 06:09:31

标签: java android sqlite button

我有一个程序可以从数据库中检索一些数据,并在屏幕上显示为一些按钮。如何为每个创建的按钮提供一个Id并将按钮.OnClickListener添加到具有正确ID的每个按钮?

这是我的代码:

private void selectAllGroup() {
// TODO Auto-generated method stub
MyGroup = (TextView) findViewById(R.id.tvListGroup);
Database allGroup = new Database(MyGroupActivity.this);
allGroup.open();
listGroup = allGroup.countHowManyGroups(username);
layout = (LinearLayout) findViewById(R.id.LLMyGroup);

String groupName[] = allGroup.fetchGroupName(username);

for (int i = 0; i < listGroup; i++) {
    newBt = new Button(this);
    newBt.setText(groupName[i]);
    layout.addView(newBt);
}

allGroup.close();
}

这是我的数据库代码(如果需要):

public int countHowManyGroups(String username) {
    // TODO Auto-generated method stub
    String Query = "SELECT " + GROUP_NAME + " From " + MS_GROUP
            + " a INNER JOIN " + MS_GROUP_DETAIL + " b ON a." + GROUP_ID
            + "=b." + GROUP_ID + " WHERE " + MEMBER_USERNAME + "=?";

    Cursor c = ourDatabase.rawQuery(Query, new String[] { username });
    int cnt = c.getCount();
    c.close();
    return cnt;

}

public String[] fetchGroupName(String username){
    int i=0;

    String Query = "SELECT " + GROUP_NAME + " From " + MS_GROUP
            + " a INNER JOIN " + MS_GROUP_DETAIL + " b ON a." + GROUP_ID
            + "=b." + GROUP_ID + " WHERE " + MEMBER_USERNAME + "=?";
    Cursor c = ourDatabase.rawQuery(Query, new String[] { username });
    String groupName[] = new String [c.getCount()];
    int iGroupName = c.getColumnIndex(GROUP_NAME);

    c.moveToFirst();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        groupName[i] = c.getString(iGroupName);
        i++;
    }

    c.close();
    return groupName;
}

3 个答案:

答案 0 :(得分:0)

你可以为for循环中的每个按钮设置标签为

    newBt.setTag(i);
    newBt.setOnClickListener(this);

并在OnClick方法内部将标记作为

的位置
    @Override
    public void onClick(View view){
        int buttonClicked = (int) view.getTag();
        // Do your code here according to the position of the button clicked
    }

答案 1 :(得分:0)

关于你的问题&#34;我怎样才能为每个创建的按钮提供一个Id并制作button.OnClickListener到每个具有正确ID的按钮?&#34;

只需在for-loop中使用:

newBt.setId(id to set) // Should be positive, doesn't have to be unique
newBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //The View carries the Id
                v.getId();
                //Then you can simply set up a fast Switch-case for example
                switch (v.getid()) {
                case 1:
                       break;
                default:
                       break;
            }
        });

<强>更新

没有问题只需将按钮转换为onClick-Method中的视图并获取字符串:

newBt.setId(id to set) // Should be positive, doesn't have to be unique
newBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Button myClickedButton = (Button) v;
                String buttonText = myClickedButton.getText();

                //Do something with your logic here. 
                //You can also Switch-case on a String ! But AFAIK 
                //it's only possible on Java Compiler 1.7 or above. 
                //But Eclipse will guide you if you want to Switch on String
        });

答案 2 :(得分:0)

for (int i = 0; i < listGroup; i++) {
    newBt = new Button(this);
    newBt.setText(groupName[i]);
   newBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               //write your logic to do the work you need when button is clicked.You do not need id for this.
            }
        });
    layout.addView(newBt);
}

您不需要ID来收听按钮点击事件。