Ich konnte nicht den Weg finden, um Listview mit Elementen zu füllen, wenn Edittext gelöscht wird und es keinen Wert für die Suche gibt. Als ich Log zum Anzeigen der Werte beim Eingeben von Zeichen verwendet habe, habe ich gesehen, dass das letzte Zeichen auch dann noch erhalten bleibt, wenn ich die editedtext-Werte gelöscht habe.Wie kann ich Listview-Elemente anzeigen, wenn Edittext für die Suche leer ist?
Ich setze Bedingung, als ob edittext.length() gleich 0 für die Überwachung von Listview-Elementen ist, wenn die Aktivitätsklasse onstart() -Methode ist. Nachdem ich etwas geschrieben und dann gelöscht habe, wird listview leer angezeigt. Hier ist mein Code-Blöcke:
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);
}
});
}
}
Und hier
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();
}
}
Wie kann ich es lösen?
Es funktioniert, thank u so much! –