【发布时间】:2016-04-01 03:18:58
【问题描述】:
我正在尝试在使用微调器选择排序样式时按字母顺序和数字顺序对列表视图中的项目进行排序。基本上,每当用户选择微调器项目时,列表视图将根据他们选择的选项进行排序。由于这是一个带有自定义适配器的自定义列表视图,因此我在寻找一种正确排序的方法时遇到了一些问题。如果有人能给我一些关于如何解决这个问题的建议,我将不胜感激。 以下是我现在处理显示列表视图的主类:
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 View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.rep_progress_view, null);
TextView exerciseName = (TextView)vi.findViewById(R.id.FullProgress);
TextView exerciseComment = (TextView)vi.findViewById(R.id.Comment);
exerciseName.setText(item.EXERCISE_NAME + " Date: " + item.DATE + " Reps: " + item.REPETITIONS + " Weight: " +item.WEIGHT + " Pounds ");
exerciseComment.setText(item.COMMENT);
return vi;
}
【问题讨论】:
-
为什么不在溢出菜单中提供排序选项。然后你有来自适配器的数据,只需按照客户端想要的排序方式排序并调用 notifydataset 更改。你想让我写一个相同的代码吗?
-
嗯...我很乐意看看你的想法。
-
在溢出菜单[即操作栏]中提供排序选项,并且一个客户端根据您将触发排序(您的 listArray )并在排序后调用 notifyDataset 更改方法来选择排序选项。
标签: android sorting listview spinner android-arrayadapter