Ursprünglich ich diesen Fehler hätte:Widersprüchliche Android-Fehlermeldungen: Das angegebene Kind hat bereits einen Elternteil. Sie müssen rufen RemoveView() auf den Eltern Kind erst
The specified child already has a parent. You must call removeView() on the child's parent first
bei
customSection.addView(customLayout);
So habe ich
((LinearLayout)customLayout.getParent()).removeView(customLayout);
und bekommen
java.lang.NullPointerException
Wenn also das Kind ein Elternteil hat und ich zuerst das Kind vom Elternteil entfernen muss, warum gibt getParent() null zurück?
Ich habe ein abstraktes Fragment, das abgeleiteten Klassen erlaubt, ein benutzerdefiniertes Layout für den Listenadapter zu liefern. Relevante Code:
Bindung:
public void bind(DataObject row) {
View customLayout = getChildItemView(row);
if (customLayout != null) {
((LinearLayout) customLayout.getParent()).removeView(customLayout);
customSection.removeAllViews();
customSection.addView(customLayout);
customSection.setVisibility(View.VISIBLE);
} else {
customLayout.setVisibility(View.INVISIBLE);
}
}
protected View getChildItemView(CommonRow row) {
if (parentView == null) {
parentView = (LinearLayout) LayoutInflater.from(getActivity())
.inflate(R.layout.list_item_custom_section,
new LinearLayout(getActivity()), true);
label = (TextView) parentView.findViewById(R.id.txtData1Label);
value = (TextView) parentView.findViewById(R.id.txtData1Value);
}
label.setText("Minimum");
value.setText(manager.formatMoney(((SpecificDataRow) row).minimum));
return parentView;
}
Ich habe auch versucht inflater.inflate(R.layout.list_item_custom_section, null)
... falsch, null/false, was ist passiert?
EDIT:
@allprog, wusste ich, einige Bereinigung nötig war. Ich schrieb das am Ende des Tages etwas in Eile. Ich habe seitdem den Code aufgeräumt und die Bindung getrennt und die Ansicht aufgepumpt. Aufgeräumt Code:
private class ViewHolder {
....
public ViewHolder(View v) {
Butterknife.inject(this, v);
View custom = createCustomView(customSection);
if (custom != null) {
customSection.setVisibility(View.VISIBLE);
customSection.addView(custom);
}
}
public void bind(CommonRow row) {
......
bindCustomView(row, customSection);
}
}
Kinderklasse:
@Override
protected View createCustomView(ViewGroup parent) {
return LayoutInflater.from(getActivity()).inflate(R.layout.list_item_custom_section, parent, false);
}
@Override
protected void bindCustomView(CommonRow row, ViewGroup section) {
TextView label = Views.findById(section, R.id.txtData1Label);
TextView value = Views.findById(section, R.id.txtData1Value);
label.setText("Minimum");
value.setText(manager.formatMoney(((SpecificRow) row).minimum));
}
suitianshi es wurde zuerst mit meinem ursprünglichen [ungepflegt] Code, der die Lösung war.
Was ist 'customSection'? Können Sie den Code anzeigen, in dem er zuerst initialisiert wird? –
Sie befindet sich in der getChildItemView-Methode (CommonRow-Zeile). parentView = (LinearLayout) LayoutInflater.from (getActivity()). inflate (R.layout.list_item_custom_section, neues LinearLayout (getActivity()), true); – Jack
Können Sie das XML für list_item_custom_section posten? Ich vermute, dass Sie versuchen, die Ansicht zu sich selbst hinzuzufügen. Außerdem müsste die Variable "customSection" immer noch deklariert (und gesetzt) werden. Kannst du diesen Code posten? –