Android TableLayout.addView()并没有真正起作用

时间:2015-01-26 20:20:50

标签: java android fragment tablelayout

我正在尝试使用addView方法用一些视图填充我的TableLayout。

这是我尝试做的事情:

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);
什么都没发生。该应用程序在此行之后无所事事。

希望有人能告诉我我的错误吗?

1 个答案:

答案 0 :(得分:0)

您没有将视图添加到TableRow,而只是将视图添加到TableLayout。试试这个:

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

    tr.addView(v);
    return tr;

}