Some Context: I have a Custom ArrayAdapter for a ListView that has 3 parameters Name, Edit Button, Delete Button. I have setup onClicks
in the ArrayAdapter to be able to detect which profile is being clicked with the specific edit/delete button press. When the user deletes a profile I remove the profile from SQLite DB
however now I've ran into the problem of trying to update the ArrayList
with the removed item and notifyDataSetChanged
for my ListView
.
Question 1: I can't figure out if I should be doing this in the Class that is containing the ListView and ArrayList or if I should be trying to update this from the ArrayAdapter in the onClicks.
Question 2: Whatever method might be right how can I correctly update the deleted item from the ListView when the user confirms the delete in the Dialog.
Current ArrayAdapter Class
public class ListViewItemAdapter extends ArrayAdapter<ListViewItem>
{
private Context mContext;
private List<ListViewItem> list = new ArrayList<>();
private DatabaseHelper databaseHelper;
private String profileName;
public ListViewItemAdapter(@NonNull Context context, ArrayList<ListViewItem> listItem) {
super(context, 0 , listItem);
mContext = context;
list = listItem;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.custom_listview,parent,false);
final ListViewItem listViewItem = list.get(position);
//Text View Profile
final TextView name = (TextView) listItem.findViewById(R.id.textView_name);
name.setText(listViewItem.getmName());
profileName = listViewItem.getmName();
//Edit Button Profile
ImageButton image = listItem.findViewById(R.id.imageView_poster);
image.setImageResource(listViewItem.getmImageDrawable());
image.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(mContext,
"Edit Profile:" + listViewItem.getmProfile() + " Name:" + listViewItem.getmName(),
Toast.LENGTH_SHORT).show();
}
});
//Delete Button Profile **Currently Testing**
ImageButton image2 = listItem.findViewById(R.id.imageView_poster2);
image2.setImageResource(listViewItem.getmImageDrawable2());
image2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
databaseHelper = new DatabaseHelper(getContext());
Toast.makeText(mContext,
"Delete Profile:" + listViewItem.getmProfile() + " Name:" + listViewItem.getmName(),
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(),R.style.AlertDialogTheme);
builder.setTitle("Delete Profile?")
.setMessage("Are you sure you want to delete\n" + listViewItem.getmName())
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
databaseHelper.deleteRowProfile(listViewItem.getmName());
//
//This is where I'm try to update the ListView
//
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
});
return listItem;
}
}
Function in Fragment Class that Populates the ListView onViewCreated
public void getProfileList()
{
arrayList = new ArrayList<ListViewItemAdapter>();
listViewItemAdapter = new ListViewItemAdapter(getContext(),arrayList);
Cursor result = databaseHelper.getAllDataCarProfile();
if(listViewItemAdapter != null){
listViewItemAdapter.clear();
listViewItemAdapter.notifyDataSetChanged();
}
if (result.getCount() != 0)
{
while (result.moveToNext())
{
arrayList.add(new ListViewItem("CarProfile",
result.getString(0),
R.drawable.ic_edit_gray_24dp,
R.drawable.ic_delete_grey_24dp));
}
listViewItemAdapter.notifyDataSetChanged();
}
listViewCarProfile.setAdapter(listViewItemAdapter);
}
You can do this in either way . You can create a function in your adapter class and perform the clickListener on it .
deleteItem.setOnClickListener(v -> {
potsList.remove(getAdapterPosition());
notifyDataSetChanged();
}
Or in your class , when remove the item from list , don't forget to notify the adapter . One the adapter get notified , it will reflect the change on screen.