在Android中,实现GridView的多条件筛选可以通过以下步骤来完成:
public class Product {
private String name;
private double price;
private String category;
// 构造函数、getter和setter方法
}
public class ProductAdapter extends BaseAdapter {
private List<Product> productList;
private Context context;
public ProductAdapter(Context context, List<Product> productList) {
this.context = context;
this.productList = productList;
}
@Override
public int getCount() {
return productList.size();
}
@Override
public Object getItem(int position) {
return productList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 绑定数据和视图
}
}
public void filter(String category, double minPrice, double maxPrice) {
List<Product> filteredList = new ArrayList<>();
for (Product product : productList) {
if (product.getCategory().equalsIgnoreCase(category) &&
product.getPrice() >= minPrice &&
product.getPrice() <= maxPrice) {
filteredList.add(product);
}
}
productList = filteredList;
notifyDataSetChanged();
}
public void onFilterButtonClick(View view) {
EditText categoryEditText = findViewById(R.id.categoryEditText);
EditText minPriceEditText = findViewById(R.id.minPriceEditText);
EditText maxPriceEditText = findViewById(R.id.maxPriceEditText);
String category = categoryEditText.getText().toString();
double minPrice = Double.parseDouble(minPriceEditText.getText().toString());
double maxPrice = Double.parseDouble(maxPriceEditText.getText().toString());
productAdapter.filter(category, minPrice, maxPrice);
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/categoryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="类别" />
<EditText
android:id="@+id/minPriceEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="最低价格"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/maxPriceEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="最高价格"
android:inputType="numberDecimal" />
<Button
android:id="@+id/filterButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="筛选"
android:onClick="onFilterButtonClick" />
</LinearLayout>
现在,当用户在输入框中输入筛选条件并点击筛选按钮时,GridView将根据这些条件进行筛选并显示符合条件的数据。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。