I have a ListView
with a CursorLoader
. The user can open ListView
items (open another Fragment
) or delete items. All database actions occur asynchronously and usually it takes a fraction of a second. But technically the user could delete an item and then open the item before the deletion callback and cause and error. What's the best way to handle this? Here are the options I see.
AsyncTask
will always happen quickly enough to avoid a problemListView
before the AsyncTask
(but this would cause a flash in the UI)AsyncTask
Edit: I ended up using RecyclerView
but I can't call adapter.notifyItemRemoved(itemPos)
until after I have deleted the item from the database.
Best option in your case will be using RecyclerView
. Because when you clicked delete you can call adapter.notifyItemRemoved(itemPos)
. So the list item will be removed with animation from your RecyclerView
and you will not worry about the delete operation result.
See the Android Developer tutorial on Creating Lists and Cards:
Use the
RecyclerView
widget when you have data collections whose elements change at runtime based on user action or network events.
Here is an example:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {
private ArrayList<String> mDataset;
private static Context sContext;
public MyAdapter(Context context, ArrayList<String> myDataset) {
mDataset = myDataset;
sContext = context;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
ViewHolder holder = new ViewHolder(v);
holder.mNameTextView.setOnClickListener(MyAdapter.this);
holder.mNameTextView.setTag(holder);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mNameTextView.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public void onClick(View view) {
ViewHolder holder = (ViewHolder) view.getTag();
if (view.getId() == holder.mNameTextView.getId()) {
**//Important !!!**
notifyItemRemoved(getPosition());
// Make your database operation also here
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mNumberRowTextView;
public TextView mNameTextView;
public ViewHolder(View v) {
super(v);
mNameTextView = (TextView) v.findViewById(R.id.nameTextView);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With