简体   繁体   English

当要搜索的Edittext为空时,如何显示Listview元素?

[英]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. 当删除edittext并且没有搜索值时,我找不到用项目填充Listview的方法。 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. 当我使用Log来监视输入字符时的值时,我看到即使删除了edittext值,最后一个字符仍然保留。

I put condition as if edittext.length() equals 0 for monitoring listview items when activity class is on onstart() method. 我将条件设置为当活动类在onstart()方法上时,edittext.length()等于0以监视列表视图项。 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. 您的filter()方法似乎不正确。

Update your ProductListAdapter as below: 如下更新您的ProductListAdapter

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查edittext是否为空? - How can I check if edittext is empty? 我怎样才能使列表视图中的edittext数据进入数据库? - How can i make data from an edittext in a listview go into the database? 如何使用我的自定义 ListView 访问 EditText - How can I access an EditText withing my custom ListView 我有一个连续包含超过1个值的列表视图,当我在Edittext中键入内容时,如何用名称值过滤列表? - I have a listview which contains more that 1 value in a row, how can i filter the list with a name value when i type in Edittext? 在将字符串搜索到我的EditText之后,如何在我的Recycler View中开始搜索匹配项 - How can I start searching for matches in my Recycler View after intenting string for search to my EditText 如何将内容保存在列表视图的edittext中 - How do I save contents in an edittext that is in a listview 当ListView滚动到底部时,如何加载新元素? - How i can load new elements when my ListView is scrolled to bottom? 仅当EditText为空时才显示警报对话框。 - Display Alert Dialog only when EditText is empty. 当我的两个EditText不为空时,可以更改按钮的笔触颜色吗? - Can I change Button stroke color when my two EditText are not empty? 我还如何使enter将edittext中的输入文本添加到列表视图? 在创建的添加按钮之上 - How can I also make enter add input text in edittext to my listview? On top of the created add button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM