从动态添加的 Button 中删除视图

Remove view from dynamically added Button

我有一些程序可以从数据库中获取和显示一些数据,并且

我想创建一个排序函数,根据发布日期、优先级和字母表对数据进行排序。 (我为此使用了微调器,因此用户将从微调器中进行选择)。

当用户更改他们的排序首选项时,我的程序将删除所有获取的数据并从数据库中重新获取新数据。

我已经成功地从数据库中获取和显示数据,并且我也成功地对数据进行了排序,但是当我想显示最新排序的数据时,视图出现了一些问题。

我的问题是所有的数据都没有完全删除,我想删除动态添加的按钮不是所有的视图..

我试过使用 layout.removeView(按钮),但它只是删除了最后创建的按钮,而不是所有按钮

这是我的代码:

public class ViewTaskToDoList extends Activity {
    userSessionManager session;
    String Username, taskId, groupId, sortBy;
    Database myDb;
    LinearLayout layout;
    Spinner spinner;
    Button newBtDate, newBtPriority, newBtAlphabet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_task_todolist);
    sessionAndDeclaration();
    dbConstructor();
}

private void dbConstructor() {
    // TODO Auto-generated method stub
    sortBy = (String) spinner.getSelectedItem();
    Toast.makeText(getApplicationContext(), sortBy, Toast.LENGTH_SHORT)
            .show();
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int index = arg0.getSelectedItemPosition();
            String[] getOption = getResources().getStringArray(
                    R.array.SortBy);
            String getSelected = getOption[index];
            myDb.open();
            int totalRow = myDb.countHowManyTaskToDoList(groupId, taskId);
            myDb.close();
            if (getSelected.equals("Posting Date")) {

                myDb.open();

                String todolistIdByDate[] = myDb
                        .fetchTaskTodolistByPostingDate(groupId, taskId);
                String[] todolistTitleByDate = new String[todolistIdByDate.length];

                for (int i = 0; i < totalRow; i++) {
                    todolistTitleByDate[i] = myDb
                            .getTodolistTitle(todolistIdByDate[i]);
                    newBtDate = new Button(ViewTaskToDoList.this);
                    newBtDate.setText(todolistTitleByDate[i]);
                    layout.addView(newBtDate);
                }

                myDb.close();
            } else if (getSelected.equals("Priority")) {

                myDb.open();
                String todolistByPriority[] = myDb
                        .fetchTaskTodolistByPriority(groupId, taskId);
                String[] todolistTitleByPriority = new String[todolistByPriority.length];

                for (int i = 0; i < totalRow; i++) {
                    todolistTitleByPriority[i] = myDb
                            .getTodolistTitle(todolistByPriority[i]);
                    newBtPriority = new Button(ViewTaskToDoList.this);
                    newBtPriority.setText(todolistTitleByPriority[i]);
                    layout.addView(newBtPriority);
                }
                myDb.close();

                Toast.makeText(getApplicationContext(),
                        "Sort based on Priority", Toast.LENGTH_SHORT)
                        .show();

            } else if (getSelected.equals("A-Z")) {

                myDb.open();

                String todolistByAlphabet[] = myDb
                        .fetchTaskTodolistByAlphabet(groupId, taskId);
                String[] todolistTitleByAlphabet = new String[todolistByAlphabet.length];

                for (int i = 0; i < totalRow; i++) {
                    todolistTitleByAlphabet[i] = myDb
                            .getTodolistTitle(todolistByAlphabet[i]);
                    newBtAlphabet = new Button(ViewTaskToDoList.this);
                    newBtAlphabet.setText(todolistTitleByAlphabet[i]);
                    layout.addView(newBtAlphabet);
                }
                myDb.close();
                Toast.makeText(getApplicationContext(),
                        "Sort based on A-Z", Toast.LENGTH_SHORT).show();

            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}

private void sessionAndDeclaration() {
    // TODO Auto-generated method stub
    session = new userSessionManager(getApplicationContext());
    HashMap<String, String> user = session.getUserDetails();

    // get name
    Username = user.get(userSessionManager.KEY_USERNAME);

    myDb = new Database(this);
    Intent getIntent = getIntent();
    groupId = getIntent.getStringExtra("passedGroupId");
    taskId = getIntent.getStringExtra("passedTaskId");
    layout = (LinearLayout) findViewById(R.id.layoutTasktodolist);
    spinner = (Spinner) findViewById(R.id.sSortBy);
}

}

我该如何解决这个问题?或者,如果您有更好的算法或代码来解决我的问题,请告诉我,谢谢

for (int i = 0; i < totalRow; i++) { todolistTitleByPriority[i] = myDb .getTodolistTitle(todolistByPriority[i]); newBtPriority = new Button(ViewTaskToDoList.this); newBtPriority.setId(i) newBtPriority.setText(todolistTitleByPriority[i]); layout.addView(newBtPriority); }

void delete(int id)
          {
Button btn=  (Button)layout.findviewbyid(id);
 btn.removeAllViews();
    }

*从父布局获取按钮ID并直接通过id移除视图*

因为您有最后一个按钮的引用(由于在您创建它的循环中重新分配了全局按钮变量)所以您只能删除最后一个 one.try 以保留引用每个动态创建的按钮,然后您将能够删除它。您可以尝试下面的代码片段。

 //declare globally
    ArrayList< Button> newBtDatelist=new ArrayList<Button>();
    //then  in your loop
  for (int i = 0; i < 5; i++) {
            //here you create button locally 
            Button btn = new Button(this);
            btn.setText("button" + i);
            layout.addView(btn);
            newBtDatelist.add(btn);
        }

要动态删除按钮,您可以这样做。

private void remove() {
        for (int i = 0; i < newBtDatelist.size(); i++) {
             layout.removeView(newBtDatelist.get(i));
        }
        newBtDatelist.clear(); //to remove all the reference from the arraylist 
    }