您好,登錄后才能下訂單哦!
在Android中,實現(xiàn)GridView的多條件篩選可以通過以下步驟來完成:
public class Product {
private String name;
private double price;
private String category;
// 構造函數(shù)、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) {
// 綁定數(shù)據(jù)和視圖
}
}
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>
現(xiàn)在,當用戶在輸入框中輸入篩選條件并點擊篩選按鈕時,GridView將根據(jù)這些條件進行篩選并顯示符合條件的數(shù)據(jù)。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。