使用Spinner Android对自定义Listview项进行排序
我试图在使用微调器选择排序样式后,按字母和数字顺序对列表视图中的项目进行排序。本质上,每当用户选择微调器项时,listview都将根据他们选择的选项进行排序。由于这是一个带有自定义适配器的自定义listview,我一直在寻找正确排序的方法时遇到一些问题。如果有人能给我一些建议,我将非常感激。 下面是关于显示listview的主类的内容:使用Spinner Android对自定义Listview项进行排序,android,sorting,listview,spinner,android-arrayadapter,Android,Sorting,Listview,Spinner,Android Arrayadapter,我试图在使用微调器选择排序样式后,按字母和数字顺序对列表视图中的项目进行排序。本质上,每当用户选择微调器项时,listview都将根据他们选择的选项进行排序。由于这是一个带有自定义适配器的自定义listview,我一直在寻找正确排序的方法时遇到一些问题。如果有人能给我一些建议,我将非常感激。 下面是关于显示listview的主类的内容: protected void onCreate(Bundle savedInstanceState) { super.onCreate(save
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_repetition_progress);
/**
* The database used to pull all exercises for this workout routine
*/
DatabaseHandler db = new DatabaseHandler(this);
SQLiteDatabase database = db.getReadableDatabase();
context = this;
/**
* Get all exercises from Triceps and Chest workout and put in arraylist
*/
ListView workoutExerciseList = (ListView)findViewById(R.id.listView7);
final List<String> arrayRepetitionProgress = new ArrayList<String>();
exListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrayRepetitionProgress);
workoutExerciseList.setAdapter(exListAdapter);
//Holds all the available exercises
final List<WorkoutTracker> exerciseList = db.getRepProgress(context, database);
/**
* Populates the listView with each exercise for this workout routine. This includes the
* each exercise name, the distance, the time of the workout, and any
* comments included.
*/
repProgressList = new ArrayList<RepetitionProgress.ListViewItem>();
for(int i = 0; i<exerciseList.size(); i++) {
final int j = i;
repProgressList.add(new ListViewItem()
{{
REPETITIONS = exerciseList.get(j).getReps();
WEIGHT = exerciseList.get(j).getWeight();
COMMENT = exerciseList.get(j).getComment();
EXERCISE_NAME = exerciseList.get(j).getExerciseName();
DATE = exerciseList.get(j).getDate();
}});
}
final RepetitionProgressAdapter adapter = new RepetitionProgressAdapter(this, repProgressList);
workoutExerciseList.setAdapter(adapter);
//Spinner used to select sorting method
SortOptions = (Spinner)findViewById(R.id.sortOption);
SortOptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//If the Sort by exercise name option is chosen
if(SortOptions.getSelectedItem().toString().equals("Sort by exercise name")) {
Toast.makeText(context.getApplicationContext(), "Sort by exercise name", Toast.LENGTH_LONG).show();
exListAdapter.sort(new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
});
//Refresh the listview
adapter.notifyDataSetChanged();
exListAdapter.notifyDataSetChanged();
}
//If the Sort by date option is chosen
if(SortOptions.getSelectedItem().toString().equals("Sort by date")) {
Toast.makeText(context.getApplicationContext(), "Sort by date", Toast.LENGTH_LONG).show();
}
//If the Sort by number of repetitions option is chosen
if(SortOptions.getSelectedItem().toString().equals("Sort by number of repetitions")) {
Toast.makeText(context.getApplicationContext(), "Sort by number of repetitions", Toast.LENGTH_LONG).show();
}
//If the Sort by weight option is chosen
if(SortOptions.getSelectedItem().toString().equals("Sort by weight")) {
Toast.makeText(context.getApplicationContext(), "Sort by weight", Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Back = (Button)findViewById(R.id.back);
Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
下面是示例代码,您可以查看一下并将其实现到您的代码中
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
// constructor Item(name, Id, weight)
items.add(new Item("Nelson", 1, 10.3));
items.add(new Item("Fisk", 2, 12.03));
items.add(new Item("Speedy", 3, 19.3));
items.add(new Item("Donna", 4, 12.3));
items.add(new Item("Matt", 5, 16.3));
items.add(new Item("Oliver", 6, 1.3));
items.add(new Item("Deadstroke", 5, 1.3));
Collections.sort(items, new MyComparator(MyComparator.NAME));
for (Item item : items) {
System.out.println("Items " + item.getName() + " \t" + item.getWeight());
}
}
static class MyComparator implements Comparator<Item> {
static final int NAME = 0, WEIGHT = 1, DATE = 2;
int type;
public MyComparator(int type) {
this.type = type;
}
@Override
public int compare(Item o1, Item o2) {
if (type == NAME) {
return o1.getName().compareTo(o2.getName());
} else if (type == WEIGHT) {
if (o1.getWeight() > o2.getWeight())
return 1;
else if (o1.getWeight() == o2.getWeight())
return 0;
else
return -1;
} else if (type == DATE) {
// now convert your date object/string to milliseconds and apply
// number logic (above)
}
return 0;
}
}
publicstaticvoidmain(字符串[]args){
列表项=新建ArrayList();
//建造商项目(名称、Id、重量)
增加(新项目(“Nelson”,1,10.3));
增加(新项目(“Fisk”,2,12.03));
增加(新项目(“快速”,3,19.3));
添加(新项目(“Donna”,4,12.3));
增加(新项目(“马特”,5,16.3));
增加(新项目(“Oliver”,6,1.3));
增加(新项目(“死冲程”,5,1.3));
Collections.sort(items,newmycomparator(MyComparator.NAME));
用于(项目:项目){
System.out.println(“Items”+item.getName()+“\t”+item.getWeight());
}
}
静态类MyComparator实现Comparator{
静态最终整数名=0,权重=1,日期=2;
int型;
公共MyComparator(int类型){
this.type=type;
}
@凌驾
公共整数比较(项目o1、项目o2){
如果(类型==名称){
返回o1.getName().compareTo(o2.getName());
}else if(类型==重量){
如果(o1.getWeight()>o2.getWeight())
返回1;
否则如果(o1.getWeight()==o2.getWeight())
返回0;
其他的
返回-1;
}else if(类型==日期){
//现在将日期对象/字符串转换为毫秒并应用
//数字逻辑(上)
}
返回0;
}
}
以及你需要如何调用Spinner
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Sort by exercise name")) {
// Collections.sort(items, new MyComparator(MyComparator.NAME));
//replace with your code/array list
} else if (selectedItem.equals("Sort by date")) {
// Collections.sort(items, new MyComparator(MyComparator.DATE));
} else if (selectedItem.equals("Sort by number of repetitions")) {
// Collections.sort(items, new MyComparator(MyComparator.REPTS));
} else if (selectedItem.equals("Sort by weight")) {
// Collections.sort(items, new MyComparator(MyComparator.WEIGHT));
}
adapter.notifyDataSetChanged();
exListAdapter.notifyDataSetChanged();
}
public void已选择(AdapterView父视图、视图视图、int位置、长id){
字符串selectedItem=parent.getItemAtPosition(position.toString();
if(selectedItem.equals(“按练习名排序”)){
//Collections.sort(items,newmycomparator(MyComparator.NAME));
//替换为代码/数组列表
}else if(selectedItem.equals(“按日期排序”)){
//Collections.sort(项目,新的mycompator(mycompator.DATE));
}else if(selectedItem.equals(“按重复次数排序”)){
//Collections.sort(items,newmycomparator(MyComparator.REPTS));
}else if(selectedItem.equals(“按重量排序”)){
//集合。排序(项目,新的MyComperator(MyComperator.WEIGHT));
}
adapter.notifyDataSetChanged();
exListAdapter.notifyDataSetChanged();
}
为什么不在溢出菜单中提供排序选项呢。然后,您就可以从适配器获取数据,只需按照客户机希望的方式进行排序并调用notifydataset change即可。你想让我写一个同样的代码吗?嗯。。。我愿意看看你的想法。在溢出菜单[即操作栏]中给出排序选项,一个客户端根据你将触发排序(你的listArray)和排序后调用notifyDataset更改方法来选择排序选项。
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Sort by exercise name")) {
// Collections.sort(items, new MyComparator(MyComparator.NAME));
//replace with your code/array list
} else if (selectedItem.equals("Sort by date")) {
// Collections.sort(items, new MyComparator(MyComparator.DATE));
} else if (selectedItem.equals("Sort by number of repetitions")) {
// Collections.sort(items, new MyComparator(MyComparator.REPTS));
} else if (selectedItem.equals("Sort by weight")) {
// Collections.sort(items, new MyComparator(MyComparator.WEIGHT));
}
adapter.notifyDataSetChanged();
exListAdapter.notifyDataSetChanged();
}