单击按钮时更改arrayadapter布局资源

时间:2012-12-24 05:51:19

标签: android layout resources listadapter

我有listview,它显示效果很好,但是当我点击“编辑”操作按钮时,我希望listview使用不同的布局资源,以便按钮显示在每个旁边列表中的项目。

layout before: listitem_routine
layout I want after "edit" action button clicked: listitem_routine_edit

继承我的代码:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_routines);

    datasource = new RoutinesDataSource(this);
    datasource.open();

    List<Routine> values = datasource.getAllRoutines();

    ArrayAdapter<Routine> adapter = new ArrayAdapter<Routine>(this,
        R.layout.listitem_routine, R.id.listitem_routine_name, values);
    setListAdapter(adapter);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      ArrayAdapter<Routine> adapter = (ArrayAdapter<Routine>) getListAdapter();
      // Handle item selection
      switch (item.getItemId()) {
      case R.id.button_routines_edit:
            // change the list view adapter resource layout to *_edit
            // hide this button and show the "done editing (checkmark)" button
          return true;
      default:
          return super.onOptionsItemSelected(item);
      }
  }

我知道R.id.button_routines_edit的情况有效,因为我在点击它时做了吐司。它很好吃我删除了那段代码......

但无论如何,点击R.id.button_routines_edit时如何更改布局资源?所有的代码都存在,我只是不知道如何更改布局资源,然后明显更新listview以显示新的行布局......

1 个答案:

答案 0 :(得分:4)

单击操作按钮后,使用新资源重新加载listview,如下所示。

  @Override
public boolean onOptionsItemSelected(MenuItem item) {

  // Handle item selection
  switch (item.getItemId()) {
  case R.id.button_routines_edit:
         ArrayAdapter<Routine> adapter = new ArrayAdapter<Routine>(this,
    R.layout.listitem_routine_edit, R.id.listitem_routine_name, values);

setListAdapter(adapter);
      return true;
  default:
      return super.onOptionsItemSelected(item);
  }
}