简体   繁体   中英

How can I display Listview elements when Edittext for searching is empty?

I couldn't find the way to fill Listview with items when edittext is deleted and there is no value to search. When I used Log for watching the values when I was entering characters, I saw that the last character remains even if I deleted edittext values.

I put condition as if edittext.length() equals 0 for monitoring listview items when activity class is on onstart() method. After I wrote something and then deleted, listview is seen empty. Here is my code blocks:

public class ProductListActivity extends Activity {

    public static final String TAG = ProductListActivity.class.getSimpleName();

    private static final String Valeron_URL = "http://www.tac.com.tr/valeron.xml";

    ProductListAdapter productListAdapter;
    ListView lvProducts;
    EditText lblSearch;
    private List<Product> productList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_productlist_temp);
        new ProductDownloaderTask(this).execute(Valeron_URL);
        initializeComponents();
    }

    private void initializeComponents() {
        lvProducts = (ListView) findViewById(R.id.lvProducts);
        lblSearch = (EditText) findViewById(R.id.lblSearch);
    }

    public void updateProducts(List<Product> products) {
        productListAdapter = new ProductListAdapter(this, products);
        lvProducts.setAdapter(productListAdapter);

        lblSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = lblSearch.getText().toString().toLowerCase(Locale.getDefault());
                productListAdapter.filter(text);
            }
        });
    }
}

And here,

public class ProductListAdapter extends BaseAdapter {

    private ProductListActivity listActivity;
    private LayoutInflater layoutInflater;
    private List<Product> products = null;
    ArrayList<Product> productList;


    public ProductListAdapter(ProductListActivity listActivity, List<Product> products) {
        this.listActivity = listActivity;
        this.layoutInflater = listActivity.getLayoutInflater();
        this.products = products;
        this.productList = new ArrayList<Product>();
        this.productList.addAll(products);
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.activity_productlist, parent, false);
        ImageView iwProduct = (ImageView) view.findViewById(R.id.iwProduct);
        TextView lblProductInfo = (TextView) view.findViewById(R.id.lblProductInfo);
        TextView lblProductColor = (TextView) view.findViewById(R.id.lblProductColor);
        final Product currentProduct = products.get(position);
        iwProduct.setImageBitmap(currentProduct.getProductImage());
        lblProductInfo.setText(currentProduct.getProductName());
        lblProductColor.setText(currentProduct.getProductColor());
        view.setTag(currentProduct);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                final Product currentProduct = products.get(position);
                Intent intent = new Intent(listActivity, ProductDetailActivity.class);
                intent.putExtra("productID", currentProduct.getProductId());
                intent.putExtra("productSKU", currentProduct.getProductSKU());
                intent.putExtra("productName", currentProduct.getProductName());
                intent.putExtra("productColor", currentProduct.getProductColor());
                intent.putExtra("productPrice", String.valueOf(currentProduct.getProductPrice()));
                intent.putExtra("productSizeDetail", currentProduct.getProductSizeDetail());
                listActivity.startActivity(intent);
            }
        });
        return view;
    }

    public void filter(String text) {
            text = text.toLowerCase(Locale.getDefault());
            products.clear();
            if (text.length() == 0) {
                this.products.addAll(products);
            } else {
                for (Product product : products) {
                    if (product.getProductName().toLowerCase(Locale.getDefault())
                            .contains(text)) {
                        this.products.add(product);
                    }
                }
            }
        notifyDataSetChanged();
    }
}

How can I solve it?

Your filter() method does not seems OK.

Update your ProductListAdapter as below:

public class ProductListAdapter extends BaseAdapter {

private ProductListActivity listActivity;
private LayoutInflater layoutInflater;
private List<Product> products;
private List<Product> productList;


public ProductListAdapter(ProductListActivity listActivity, List<Product> products) {
    this.listActivity = listActivity;
    this.layoutInflater = listActivity.getLayoutInflater();
    this.products = products;
    this.productList = new ArrayList<Product>();
    this.productList.addAll(products);
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = layoutInflater.inflate(R.layout.activity_productlist, parent, false);
    ImageView iwProduct = (ImageView) view.findViewById(R.id.iwProduct);
    TextView lblProductInfo = (TextView) view.findViewById(R.id.lblProductInfo);
    TextView lblProductColor = (TextView) view.findViewById(R.id.lblProductColor);
    final Product currentProduct = products.get(position);
    iwProduct.setImageBitmap(currentProduct.getProductImage());
    lblProductInfo.setText(currentProduct.getProductName());
    lblProductColor.setText(currentProduct.getProductColor());
    view.setTag(currentProduct);
    view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            final Product currentProduct = products.get(position);
            Intent intent = new Intent(listActivity, ProductDetailActivity.class);
            intent.putExtra("productID", currentProduct.getProductId());
            intent.putExtra("productSKU", currentProduct.getProductSKU());
            intent.putExtra("productName", currentProduct.getProductName());
            intent.putExtra("productColor", currentProduct.getProductColor());
            intent.putExtra("productPrice", String.valueOf(currentProduct.getProductPrice()));
            intent.putExtra("productSizeDetail", currentProduct.getProductSizeDetail());
            listActivity.startActivity(intent);
        }
    });
    return view;
}

public void filter(String text) {
        text = text.toLowerCase(Locale.getDefault());
        products.clear();
        if (text.length() == 0) {
            this.products.addAll(productList);
        } else {
            for (Product product : productList) {
                if (product.getProductName().toLowerCase(Locale.getDefault())
                        .contains(text)) {
                    this.products.add(product);
                }
            }
        }
    notifyDataSetChanged();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM