Im creating a listView and setting a custom arrayAdapter to it in my main activity. Using an onClickListener within the getView method of the adapter to try and remove the item clicked. Its being removed from the array in the adapter but i cant make the listview refresh to show that using notifyDataSetChanged.
Ive tried using an onItemClickListener in the main activity instead but i can only get that to work by setting the items in the object view to not clickable which wont work because of the checkboxes.
ive tried calling notifyDataSetChanged in the onClickListener in getView, at the end of getView itself but neither do anything.
the only way ive gotten the listview to change is creating a new instance of the adapter and setting the listview again which i do in the main activity to add new items, but i dont know if i could do that from the adapter and i cant set onClick for the listview in main activity because of above.
i know the data is being removed from the array because the log lines ADAPTER ONCLICK START and END show the removal, but i cannot find a way to update the screen at that moment.
heres the main activity
public class MainActivity extends AppCompatActivity {
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<TaskModel> tasks = read();
//listview
ListView listView = findViewById(R.id.task_list);
adapter = new CustomAdapter(this, tasks);
listView.setAdapter(adapter);
//add button
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.add_dialog,null);
final EditText txt_inputText = mView.findViewById(R.id.editText);
builder.setView(mView);
builder.setMessage("add a new item").setPositiveButton("Add", (dialog, which) -> {
//do the add here
Log.d("NEW START", String.valueOf(tasks.size()));
tasks.add(new TaskModel(txt_inputText.getText().toString(), false));
write(tasks);
adapter = new CustomAdapter(this, tasks);
listView.setAdapter(adapter);
Toast.makeText(getApplicationContext(),"new item added",Toast.LENGTH_SHORT).show();
Log.d("NEW END", String.valueOf(tasks.size()));
}).setNegativeButton("Cancel", (dialog, which) -> Toast.makeText(getApplicationContext(),"new item cancelled",Toast.LENGTH_SHORT).show());
AlertDialog alert = builder.create();
alert.show();
});
}
}
and the adapter
public class CustomAdapter extends ArrayAdapter{
Context mContext;
ArrayList<TaskModel> data;
public CustomAdapter(Context context, ArrayList<TaskModel> data) {
super(context, R.layout.list_item, data);
this.mContext = context;
this.data = data;
}
public static class ViewHolder{
TextView txtName;
CheckBox chkBox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Log.d("ADAPTER START", String.valueOf(data.size()));
TaskModel model = (TaskModel) getItem(position);
ViewHolder viewHolder;
if(convertView == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.txtName = convertView.findViewById(R.id.textView);
viewHolder.chkBox = convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtName.setOnClickListener(v -> {
Log.d("ADAPTER ONCLICK START", String.valueOf(data.size()));
for(int i =0; i < data.size(); i++){
if(viewHolder.txtName.getText().equals(data.get(i).name)){
data.remove(i);
}
}
write(data);
refresh();
Log.d("ADAPTER ONCLICK END", String.valueOf(data.size()));
});
viewHolder.chkBox.setOnCheckedChangeListener((buttonView, isChecked)->{
if(isChecked){
//do checked
}else{
//do unchecked
}
});
viewHolder.txtName.setText(model.name);
viewHolder.chkBox.setChecked(model.done);
return convertView;
}
}