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

时间:2015-01-02 08:03:52

标签: android sorting view

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

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

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

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

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

我尝试过使用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);
}

}

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

2 个答案:

答案 0 :(得分:1)

因为您已经引用了最后一个按钮(由于您在创建它的循环中重新分配了全局按钮变量),这就是为什么您只能删除最后一个按钮。以保留参考每个动态创建的按钮,然后您将能够删除它。你可以尝试下面的代码片段。

 //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 
    }

答案 1 :(得分:0)

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 * *