android Listview删除一个项目并刷新列表

时间:2017-03-12 16:55:37

标签: android listview post retrofit response

我有listview,其中包含文本和按钮。当我单击删除按钮时,删除列表视图项。 İts停止。我使用的是API,而不是SQLite;并删除(); notifyDataSetChanged();但我无法工作。单击删除按钮时,我将ProductID发送到setFavoriteMerchant以获取删除项目。我只是发送响应API并获取它。 非常感谢所有帮助的人

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

1 个答案:

答案 0 :(得分:0)

问题是你在这里设置一个整数convertView.setTag(item.getID());在检索你的viewHolder时,你试图将其强制转换为viewHolder =(FavoriteMerchantAdapter.ViewHolder)convertView.getTag();

解决方案:

替换:convertView.setTag(item.getID());

使用:convertView.setTag(viewHolder);

问题2:ListView没有刷新

而不仅仅是 FavoriteMerchantAdapter.this.notifyDataSetChanged();  您需要清除列表并再次添加,然后调用notifyDataSetChanged。

某些地方:

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