我正在学习制作应用程序,这是我的第一个项目。我已经创建了自定义列表,其中包括TextView
和CheckBox
。我想删除点击的项目。
我知道通常我会输入:
values.remove(position);
myAdapter.notifyDataSetChanged();
但是因为我有一个自定义列表,所以它不起作用。
public class SecondActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener{
private Button btn;
private EditText itemET;
private ListView itemslist2;
private ArrayList<String> values;
private ArrayAdapter<String> myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
itemET = findViewById(R.id.item_edit_text2);
btn = findViewById(R.id.add_btn);
itemslist2 = findViewById(R.id.listv);
values = FileHelper.readData(this);
// Creating adapter for custom ListView
myAdapter = new ArrayAdapter(this, R.layout.listlayout, R.id.label, values);
itemslist2.setAdapter(myAdapter);
btn.setOnClickListener(this);
itemslist2.setOnItemClickListener(this);
}
//Action to add text from EditText(itemET) to our list
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_btn: {
String checkItemEntered;
checkItemEntered = itemET.getText().toString();
if ("".equals(checkItemEntered)) {
break;
} else {
String itemEntered = itemET.getText().toString();
myAdapter.add(itemEntered);
itemET.setText("");
FileHelper.writeData(values, this);
break;
}
}
}
}
//Deleting items from list
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
values.remove(position);
myAdapter.notifyDataSetChanged();
FileHelper.writeData(values, this);
}
}