![](/img/trans.png)
[英]Android: How to remove view from linearlayout from another class?
[英]How to remove return list view from parent linearlayout?
我有MainActivity和ListClassFile。
MainActivity具有XML的Linearlayout(名稱為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
。 並非recordContent中的所有視圖。
如何指定刪除listview (rdActivity.setListHandler(MainActivity.this) )?
非常感謝你。
代替電話
recordContent.removeAllViews();
呼叫:
recordContent.removeView(rdActivity);
或者,您可以獲取數量並刪除最后一個:
int num = recordContent.getChildCount();
recordContent.removeViewAt(num-1);
將返回的listview分配給實例變量,在刪除時使用它。
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);**
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.