要使用Spinner對自定義ListView項進行排序,你需要完成以下步驟:
在布局文件中添加一個Spinner組件,并定義一個適配器來為其提供數據。
創建一個用于排序的函數,該函數將根據用戶選擇的排序選項對ListView項進行排序。
在Activity中,獲取Spinner的引用,并設置一個OnItemSelectedListener來監聽選項的變化。
在OnItemSelectedListener中,根據選項的變化調用排序函數,然后更新ListView的適配器。
下面是一個示例代碼,來演示如何使用Spinner對自定義ListView項進行排序:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Spinner spinner;
private CustomListAdapter adapter;
private ArrayList<Item> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
spinner = findViewById(R.id.spinner);
// 創建一個包含數據的ArrayList<Item>
itemList = new ArrayList<>();
itemList.add(new Item("Item 1", 10));
itemList.add(new Item("Item 2", 5));
itemList.add(new Item("Item 3", 8));
itemList.add(new Item("Item 4", 3));
itemList.add(new Item("Item 5", 12));
// 創建適配器并設置給ListView
adapter = new CustomListAdapter(this, itemList);
listView.setAdapter(adapter);
// 創建一個排序函數
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.sort_options, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
// 根據排序選項對itemList進行排序
if (position == 0) {
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item1, Item item2) {
return item1.getName().compareTo(item2.getName());
}
});
} else if (position == 1) {
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item1, Item item2) {
return Integer.compare(item1.getValue(), item2.getValue());
}
});
}
// 更新ListView的適配器
adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// 未選擇任何選項
}
});
}
}
CustomListAdapter.java:
public class CustomListAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> itemList;
public CustomListAdapter(Context context, ArrayList<Item> itemList) {
super(context, R.layout.list_item, itemList);
this.context = context;
this.itemList = itemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView nameTextView = rowView.findViewById(R.id.nameTextView);
TextView valueTextView = rowView.findViewById(R.id.valueTextView);
Item item = itemList.get(position);
nameTextView.setText(item.getName());
valueTextView.setText(String.valueOf(item.getValue()));
return rowView;
}
}
Item.java:
public class Item {
private String name;
private int value;
public Item(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@