当删除edittext并且没有要搜索的值时,我找不到用项目填充Listview的方法。当我在输入字符时使用Log查看值时,即使删除了edittext值,我也看到最后一个字符仍然存在。
当活动类在onstart()方法上时,我把条件好像edittext.length()等于0来监视listview项目。在我写了一些然后删除之后,listview被看作是空的。这是我的代码块:
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);
}
});
}
}
在这里,
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();
}
}
我该如何解决?
答案 0 :(得分:0)
您的filter()
方法似乎不合适。
更新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();
}