您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。