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

标签 android listview post retrofit response

我有一个包含文本和按钮的 ListView 。当我单击删除按钮时,用于删除 ListView 项。它停止了。我使用的是 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

最佳答案

问题是你在这里设置了一个整数 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();
}

关于android Listview删除一个项目并刷新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750273/

相关文章:

Android - 如何接收快捷方式创建结果

android - 如何在 Android Studio (gradle) 中通过 LINK 引用单个 native 库(.so 文件)

android - 如何限制 ListView 中的复选框选择?

java - 使用预填充或外部数据库在 ListView 中添加 OnClickListener

ruby - 获取 Ruby Net::HTTP POST 的上传进度

http - 通过 HTTP Post 发送 XML 时编码 "<"和 ">"

Android Eclipse ddms线程 View 透视图

android - Android 上 Google 数据安全的收集数据和共享数据之间的区别?

android - ListView 页脚仅在首先选择 listview 项后可选择

java - DefaultHttpClient、证书、Https 和发布问题!