如何仅使用新值刷新Android Listview

时间:2016-06-10 06:45:33

标签: android listview

我正在尝试在listview中设置我更新的适配器,但我有一个问题是列表视图中的旧值仍然保留在那里,并且设置了新值,所以我只想在listview中设置新的刷新值。 / p>

1 个答案:

答案 0 :(得分:1)

试试这个,

这是产品列表包含产品名称和费用的示例。

我认为ProductDetails实体类包含产品名称和价格

public class ProductDetails {
    String productName;
    int productPrice;

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(int productPrice) {
        this.productPrice = productPrice;
    }
}

MainActivity.java添加此代码

        Button updateCostList = (Button)findViewById(R.id.btnUpdateCostList);
        ListView costList = (ListView)findViewById(R.id.costList);
        // create instance of custom adapter for custom list view
        customCostlistAdapter = new CustomCostlistAdapter();
        costList.setAdapter(customCostlistAdapter);

        // when we user click on update list details
        updateCostList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // we have set adapter properly
                if (customCostlistAdapter != null && customCostlistAdapter.getCount() > 0){

                    // update 2nd element from arraylist 
                    ProductDetails productDetails = new ProductDetails();
                    productDetails.setProductName("Updated product");
                    productDetails.setProductPrice(5555);
                    productCostDetails.set(2,productDetails);

                    // make changes in list
                    // this is magical line
                    customCostlistAdapter.notifyDataSetChanged();
                }
            }
        });

现在为产品费用详细信息创建自定义适配器(对于自定义列表视图)

    /**
     * Custom class for set product price details
     */
    private class CustomCostlistAdapter extends BaseAdapter{
        LayoutInflater layoutInflater = getLayoutInflater();
        @Override
        public int getCount() {
            return productCostDetails.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View productCostDetailView = layoutInflater.inflate(R.layout.custom_product_costview,null);
            // create view for product
            TextView productName = (TextView)productCostDetailView.findViewById(R.id.txtProductName);
            TextView productPrice = (TextView)productCostDetailView.findViewById(R.id.txtProductPrice);
            //set values
            productName.setText(productCostDetails.get(position).getProductName());
            productPrice.setText(productCostDetails.get(position).getProductPrice()+"");

            return productCostDetailView;
        }
    }