0

我有 MainActivity 和 ListClassFile 。

MainActivity 具有来自 XML 的 Linearlayout(name is recordContent) 组件。(使用 findviewById 获取)。

在 ListClassFile 文件中,我调用 setListHandler 将返回 ListView 类。

现在,我在 MainActivity 中有两个按钮。当我单击 A 按钮时。

它会调用

private ListClassFile rdActivity = null;
private LinearLayout recordContent = null;
....
@Overrid
protected void onCreate(...){
...
  recordContent = (LinearLayout) findViewById(R.id.recordContent);

    ...
    if( rdActivity == null )
        {
            rdActivity = new ListClassFile();
            recordContent.addView( rdActivity.setListHandler(MainActivity.this) ); 

           // rdActivity.setListHandler(MainActivity.this) it will return listview

        }
    }
    ...
}

listview显示。

当我单击 B 按钮时。

我会打电话

   if( rdActivity != null )
          {
                rdActivity = null;
                recordContent.removeAllViews();
          }

它将成功删除。

但我只想删除listview. 并非记录内容中的所有视图。

如何指定删除 listview (rdActivity.setListHandler(MainActivity.this) )?

非常感谢你。

4

2 回答 2

0

而不是打电话

recordContent.removeAllViews();

称呼:

recordContent.removeView(rdActivity);

或者您可以获取孩子的数量并删除最后一个:

int num = recordContent.getChildCount();
recordContent.removeViewAt(num-1);
于 2013-06-18T08:31:34.013 回答
0

将返回的列表视图分配给实例变量,在删除时使用它。

private ListClassFile rdActivity = null;
private LinearLayout recordContent = null;
private ListView lv;
....
@Overrid
protected void onCreate(...){
...
  recordContent = (LinearLayout) findViewById(R.id.recordContent);

    ...
    if( rdActivity == null )
        {
            rdActivity = new ListClassFile();
            lv = rdActivity.setListHandler(MainActivity.this);
            recordContent.addView(lv); 

           // rdActivity.setListHandler(MainActivity.this) it will return listview

        }
    }
    ...
}

在点击 B 按钮

if( rdActivity != null )
{
rdActivity = null;
**recordContent.removeView(lv);**
}
于 2013-06-18T08:35:46.227 回答