我有一個包含複選框和EditText字段的列表項的自定義佈局。點擊添加按鈕後,儘管新項目正在添加,但清單已被清除。所有以前的數據都消失了。我是新至Android所以請讓我知道我錯了......添加新行時,所有ListView項目都會被清除
這裏是我的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() ;
}
});
}
}
這是我的適配器類:
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;
}
}
Item類:
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) ;
}
}
我試過這個,但它仍然不工作.. – heytherebrowncow