2015-01-26 71 views
0

我想用一些視圖填充我的TableLayout與方法addView。Android的TableLayout.addView()不真的工作

這是我嘗試做:

private void createNewTable(){ 

    tableLayout = (TableLayout) getView().findViewById(R.id.tableLayout1); 
    for(int i = 0; i < objectList.size(); i++){ 
      if(objectList.get(i).getType() == 0){ 
       tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i); 
      } 
    } 
} 

和createObjectInList方法:

private View createLocationObjectInTable(LocationObject locObject) { 

    TableRow tr = new TableRow(getActivity()); 
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false); 

    TextView textViewCityName = (TextView) v.findViewById(R.id.textView_City_Name); 
    TextView textViewCityProvider = (TextView) v.findViewById(R.id.textView_City_Provider); 
    textViewCityName.setText(locObject.getTitle()); 
    textViewCityProvider.setText(locObject.getSubTitle()); 

    return v; 
} 

但意見沒有在tableLayout顯示。 logcat的沒有給我任何的錯誤消息,當我嘗試行

tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i); 

什麼happend後做的東西。這條線後,該應用程序什麼都不做。

希望任何人都可以告訴我我的錯誤?

+0

你不是應該返回從你createLocationObjectInTable方法而不是膨脹的視圖V的的TableRow視圖(TR)? – 2015-01-26 20:26:08

+0

即使我返回tr,視圖也不會顯示。我從AsyncTask調用createNewtable()方法,也許這會導致一些問題? – RyuZz 2015-01-26 20:42:07

回答

0

您不會將視圖添加到TableRow,而只需將視圖添加到TableLayout。試試這個:

private View createLocationObjectInTable(LocationObject locObject) { 
    ... 
    textViewCityProvider.setText(locObject.getSubTitle()); 

    tr.addView(v); 
    return tr; 

}

+0

我試過這個,但是在我返回tr之後,TableLayout仍然沒有顯示任何視圖。 tableLayout.addView之後的代碼仍然沒有運行,應用程序停止。 – RyuZz 2015-01-26 20:39:32

+0

@Sascha_你把你的視圖添加到TableRow('tr.addView(v);')?而「停止」意味着什麼?活動在LogCat中沒有啓動沒有錯誤? – fRoStBiT 2015-01-26 20:54:41