【问题标题】:LinearLayout not updating with TableLayout programmaticallyLinearLayout 未以编程方式使用 TableLayout 更新
【发布时间】:2013-03-15 00:30:22
【问题描述】:

我有一个 LinearLayout,我使用代码以编程方式使用 TableLayout 进行更新:

public void updateTableView(MyData data){
  DataTable dataTable = new DataTable(getApplicationContext(), data);
  LinearLayout placeHolder = (LinearLayout) findViewById(R.id.rel_view);
  placeHolder.addView(dataTable);
}

(更多背景见Android RelativeLayout alignment based on View added programmatically。)

第一次调用 updateTableView 方法时它工作正常。但是 LinearLayout 似乎忽略了所有后续调用。我知道这一点是因为 data 每次都不同,但应用程序只显示第一次调用 updateTableView 的结果,即视图没有改变。

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    可能是因为您没有删除之前添加的DataTable 视图。尝试拨打removeView()

    private DataTable dataTable;
    
    public void updateTableView(MyData data) {
      if (dataTable != null) {
          placeHolder.removeView(dataTable);
      }
      dataTable = new DataTable(getApplicationContext(), data);
      LinearLayout placeHolder = (LinearLayout) findViewById(R.id.rel_view);
      placeHolder.addView(dataTable);
    }
    

    【讨论】:

    • 酷!它现在正在工作。我只是为我的具体情况打电话给removeAllViews()
    • 太棒了。抱歉,我的代码中有一个错误,应该首先调用 removeView (以便它删除旧的而不是新的)。我现在更新了我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2010-12-04
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多