Search code examples
androidlistviewpostretrofitresponse

android Listview delete an item and Refresh list


I have listview which contains texts and buttons. When i click delete button, for delete an listview item. İts stop. I used API, not SQLite; and remove(); notifyDataSetChanged(); but i cant work it. I send ProductID to setFavoriteMerchant for delete item when click delete button. I just send response API and get it. Big thanks to everyone who help

public class FavoriteMerchantActivity extends AppCompatActivity {

    FavoriteMerchantAdapter favoriteMerchantAdapter;

    @InjectView(R.id.ListFavoriteMerchant)
    ListView ListFavoriteMerchant;

    AlertDialogHelper alertDialogHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite_merchant);
        ButterKnife.inject(this);
        alertDialogHelper = new AlertDialogHelper(this);

        getFavoriteMerchant();
    }

    void getFavoriteMerchant() {

        Call<FavoriteMerchant> call = ToolApi.getApi().getFavoriteMerchant(BaseService.TOKEN);
        call.enqueue(new Callback<FavoriteMerchant>() {
            @Override
            public void onResponse(Response<FavoriteMerchant> response, Retrofit retrofit) {

                if (response.body() != null) {
                    FavoriteMerchant favoriteMerchant = response.body();

                    Integer errorCode = favoriteMerchant.getStatus().getErrorCode();

                    if (errorCode == 0) {

                        List<ItemsFavoriteMerchant> list = favoriteMerchant.getItems();

                        favoriteMerchantAdapter = new FavoriteMerchantAdapter(alertDialogHelper, favoriteMerchant, list, getApplicationContext());
                        ListFavoriteMerchant.setAdapter(favoriteMerchantAdapter);
                    }

                } else {
                    startActivity(getIntent());
                    alertDialogHelper.showAlertError("Connection error...");
                }

            }

            @Override
            public void onFailure(Throwable t) {
                startActivity(getIntent());
                alertDialogHelper.showAlertError("Connection error...");
            }
        });
    }
}

public class FavoriteMerchantAdapter extends BaseAdapter {

    List<ItemsFavoriteMerchant> itemsFavoriteMerchant;
    FavoriteMerchant favoriteMerchant;
    Context context;
    AlertDialogHelper alertDialogHelper;

    public FavoriteMerchantAdapter(AlertDialogHelper alertDialogHelper, FavoriteMerchant favoriteMerchant, List<ItemsFavoriteMerchant> itemsFavoriteMerchant, Context context) {
        this.favoriteMerchant = favoriteMerchant;
        this.context = context;
        this.itemsFavoriteMerchant = itemsFavoriteMerchant;
        this.alertDialogHelper = alertDialogHelper;
    }

    @Override
    public int getCount() {
        return itemsFavoriteMerchant.size();
    }

    @Override
    public Object getItem(int position) {
        return itemsFavoriteMerchant.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FavoriteMerchantAdapter.ViewHolder viewHolder;
        String CDnPath = favoriteMerchant.getCDNPath();
        ItemsFavoriteMerchant item = itemsFavoriteMerchant.get(position);
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            viewHolder = new FavoriteMerchantAdapter.ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_favorite_merchant, null);

            viewHolder.FavoriteMerchantPlaceDesc = (TextViewGothamBook) convertView.findViewById(R.id.FavoriteMerchantPlaceDesc);
            viewHolder.FavoriteMerchantLocationDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantLocationDesc);
            viewHolder.FavoriteMerchantCashDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantCashDesc);
            viewHolder.FavoriteMerchantDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantDesc);
            viewHolder.FavoriteMerchantLogo = (ImageView) convertView.findViewById(R.id.FavoriteMerchantLogo);
            viewHolder.FavoriteMerchantDeleteButton = (ImageButton) convertView.findViewById(R.id.im_btn_deletemerchant);
            viewHolder.FavoriteMerchantDeleteButton.setTag(position);
            viewHolder.FavoriteMerchantDeleteButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    setFavoriteMerchant((int) v.getTag());
                    FavoriteMerchantAdapter.this.notifyDataSetChanged();
                    alertDialogHelper.showAlertSuccess("Removed from your favorites!");

                }
            });
            convertView.setTag(item.getID());
        } else {
            viewHolder = (FavoriteMerchantAdapter.ViewHolder) convertView.getTag();
        }
        return convertView;
    }


    public static class ViewHolder {

        ImageView FavoriteMerchantLogo;
        TextViewGothamBook FavoriteMerchantPlaceDesc;
        TextViewGothamMedium FavoriteMerchantLocationDesc;
        TextViewGothamMedium FavoriteMerchantCashDesc;
        TextViewGothamMedium FavoriteMerchantDesc;
        ImageButton FavoriteMerchantDeleteButton;

    }


    public void setFavoriteMerchant(final int index) {

        Call<SetFavoriteMerchant> call = ToolApi.getApi().setFavoriteMerchant(BaseService.TOKEN, itemsFavoriteMerchant.get(index).getID(), true);
        call.enqueue(new Callback<SetFavoriteMerchant>() {
            @Override
            public void onResponse(Response<SetFavoriteMerchant> response, Retrofit retrofit) {
                itemsFavoriteMerchant.remove(index);
            }
            @Override
            public void onFailure(Throwable t) {
                alertDialogHelper.showAlertError("connection error...");
            }
        });


    }
}

enter image description here


Solution

  • The problem is you are setting an integer here convertView.setTag(item.getID()); and while retrieving your viewHolder you are trying to cast that back here viewHolder = (FavoriteMerchantAdapter.ViewHolder) convertView.getTag();

    Solution:

    Replace : convertView.setTag(item.getID());

    With : convertView.setTag(viewHolder);

    Problem 2: ListView not getting refreshed

    Instead of just FavoriteMerchantAdapter.this.notifyDataSetChanged(); you need to clear the list and add it again and then call notifyDataSetChanged.

    Somewhere down the lines of :

     public void updateFavouritesList(List<ItemsFavoriteMerchant> updatedList) {
        itemsFavoriteMerchant.clear();
        itemsFavoriteMerchant.addAll(updatedList);
        FavoriteMerchantAdapter.this.notifyDataSetChanged();
    }