Ich habe ein benutzerdefiniertes Layout für das Listenelement, das ein Kontrollkästchen und ein EditText-Feld enthält. Wenn Sie auf die Schaltfläche Hinzufügen klicken, wird zwar ein neues Objekt hinzugefügt, aber die Liste wird gelöscht. Alle vorherigen Daten verschwinden. Ich bin so auf Android lass es mich wissen, wo ich falsch werde ...Alle ListView-Elemente werden gelöscht, wenn eine neue Zeile hinzugefügt wird
Hier ist meine MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ArrayList for items
final ArrayList<Item> itemArrayList = new ArrayList<>() ;
// CheckBox and EditText
CheckBox checkBox = (CheckBox) findViewById(R.id.default_checkbox) ;
EditText editText = (EditText) findViewById(R.id.default_edit_text) ;
// Add Button
Button addButon = (Button) findViewById(R.id.add_button);
// Item Adapter
final ItemAdapter itemAdapter = new ItemAdapter(this, itemArrayList) ;
// ListView Object
final ListView listView = (ListView) findViewById(R.id.list) ;
// Set Adapter
listView.setAdapter(itemAdapter);
// Setting the onClick Listener
addButon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cBox = new CheckBox(MainActivity.this);
//EditText eText = new EditText(MainActivity.this) ;
itemArrayList.add(new Item(cBox, "")) ;
itemAdapter.notifyDataSetChanged() ;
}
});
}
}
Und hier ist meine Adapter Klasse:
public class ItemAdapter extends ArrayAdapter<Item> {
// Arraylist
private ArrayList<Item> itemsArrayList = new ArrayList<>() ;
//Constructor
/*@param context This is the class which is sent as the context
* @param items This is the ArrayList
* @param resource Extra elements*/
public ItemAdapter(@NonNull Context context, ArrayList<Item> items) {
super(context,0, items);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView ;
if (listView == null) {
listView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Item at index position
Item currentItem = getItem(position) ;
// Initializing elements
CheckBox checkBox = (CheckBox) listView.findViewById(R.id.default_checkbox);
EditText editText = (EditText) listView.findViewById(R.id.default_edit_text);
// Setting the state of CheckBox
if (currentItem.getCheckBox().isChecked()) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
// Setting the state of EditText
editText.setText(currentItem.getCheckBoxText());
return listView;
}
}
Die Item-Klasse:
public class Item {
// CheckBox
private CheckBox checkBox ;
// Text
private String checkBoxText ;
// Constructor
//* @param rootView This is the layout which contains the checkBox and editText element
//* @param cBox This is the checkBox
//* @param text This is the text alongside the checkBox
public Item(CheckBox cBox, String text){
// Initializing the Item
checkBox = cBox ;
checkBoxText = text ;
}
// Method to get checkBox
public CheckBox getCheckBox(){
return checkBox ;
}
// Method to get checkBoxText
public String getCheckBoxText(){
return checkBoxText ;
}
// Method to set CheckBox
/*
* @param cBox This is the checkBox which is set*/
public void setCheckBox(CheckBox cBox){
checkBox = cBox ;
}
// Method to set checkBoxText
public void setCheckBoxText(String text){
checkBoxText = (text) ;
}
}
Ich habe das versucht, aber es funktioniert immer noch nicht .. – heytherebrowncow