我正在使用此https://github.com/ISchwarz23/SortableTableView库在自定义TableView中显示数据。我做的是我将自定义View从xml传递给UserTableDataAdapter类,但是当Fragment启动时,我在stacktrace中收到此错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的代码:
片段类:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DataBaseHelper helper = new DataBaseHelper(getActivity());
allUsers = helper.getAllUsers();
v = inflater.inflate(R.layout.fragment_fragment_add_user, container, false);
addEditIcons = inflater.inflate(R.layout.add_edit_icons, container, false); // CUSTOM VIEW THAT I PASS TO UserTableDataAdapter
TableView tableView = (TableView<String[]>) v.findViewById(R.id.tableView);
tableView.setColumnCount(2);
tableView.setDataAdapter(new UserTableDataAdapter(getActivity(), allUsers, addEditIcons)); // HERE I PASS VIEW
SimpleTableHeaderAdapter simpleHeader = new SimpleTableHeaderAdapter(getActivity(), TABLE_HEADERS);
simpleHeader.setTextColor(Color.WHITE);
simpleHeader.setPaddingTop(8);
simpleHeader.setPaddingBottom(8);
tableView.setHeaderAdapter(simpleHeader);
tableView.setHeaderElevation(10);
int colorEvenRows = getResources().getColor(R.color.white);
int colorOddRows = getResources().getColor(R.color.et_grey);
tableView.setDataRowBackgroundProvider(TableDataRowBackgroundProviders.alternatingRowColors(colorEvenRows, colorOddRows));
tableView.addDataClickListener(new UserClickListener());
return v;
}
UserTableDataAdapter 类:
class UserTableDataAdapter extends TableDataAdapter<User> {
View v;
public UserTableDataAdapter(Context context, List<User> data, View v) { // HERE I INITIALIZE CUSTOM VIEW
super(context, data);
this.v = v;
}
@Override
public View getCellView(int rowIndex, int columnIndex, ViewGroup parentView) {
User user = getRowData(rowIndex);
View renderedView = null;
switch (columnIndex) {
case 0:
renderedView = showLayout(user);
break;
}
return renderedView;
}
private View showLayout (final User user) {
LinearLayout iconsLayout = v.findViewById(R.id.iconsLayout);
TextView tvRow = v.findViewById(R.id.tvRow);
tvRow.setText(user.getSurname().toString());
return v;
}
答案 0 :(得分:0)
我不熟悉这个库,但我所有的/api/applicants/:id/fullDetails
样式方法都应该返回一个新的视图,所以你可能需要改变你的方法看起来像这样。
getView()
这也可能不相关,但您需要确保private View showLayout (final User user) {
LinearLayout iconsLayout = v.findViewById(R.id.iconsLayout);
TextView tvRow = v.findViewById(R.id.tvRow);
tvRow.setText(user.getSurname().toString());
return iconsLayout ;
}
的所有路径都返回非空视图。