经过几个小时的搜索,我不知道如何使比较器变得可以。 我发现这个“Parcelable stuff”有点“缺少内容”......(Parcelable甚至没有writeBoolean)
public class T implements Parcelable{
...
public static Comparator<T> Compare = new Comparator<T>() {...}
...
public void writeToParcel(Parcel out, int flags) {
//how to write "Compare"
}
private T(Parcel in) {
//how to read "Compare"
}
}
谢谢。
编辑:
所以这里是关于为什么F类必须是可分解的细节(简化和改编)。请注意,代码来自完成的iOS项目。如果架构保持不变,那将对维护有益......: - )
public class F implements Parcelable{
…
public ArrayList<T> listArray;
public HashMap<String,Comparator<T>> sortElementMap;
}
_
public class T implements Parcelable{
…
public static Comparator<T> CompareByName = new Comparator<T>() {…}};
public static Comparator<T> CompareByDate = new Comparator<T>() { … }};
}
首先,在一个配置类(这里简化)中,允许轻松地适应应用程序(T的变量之间的列数据库匹配,过滤/排序/搜索定义,......)
F f=new F();
f.sortElementMap.put("name",T.CompareByName)
f.sortElementMap.put("date",T.CompareByDate)
在上一个活动中
Intent intent=new Intent(…);
intent.putExtra("F",F);
startActivity(intent);
在当前活动中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
…
Intent intent = getIntent();
f = (F)intent.getParcelableExtra("F");
}
// the list to be sorted
listArray=new ArrayList<String>(f.listArray);
listAdapter = new ListAdapter(this, R.layout.list_item, listArray);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//@Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
…
}
});
// the list with the different type of sorting
sortArray=new ArrayList<String>();
Iterator it = f.sortElementMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
sortArray.add((String) pairs.getKey());
}
sortListAdapter = new ListAdapter(this, R.layout.list_item_sort, sortArray);
sortListView = (ListView) findViewById(R.id.sortListView);
sortListView.setAdapter(sortListAdapter);
sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//@Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
applyCurrentSelectedSort();
}
});
void applyCurrentSelectedSort(){
Iterator it = f.sortElementMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
for (int i = 0; i < sortListAdapter.getSelectedPosition().length; i++) {
if (sortListAdapter.getSelectedPosition()[i]) {
listAdapter.sort((java.util.Comparator<T>) pairs.getValue());
break;
}
}
}
}
答案 0 :(得分:1)
public enum Comparators {
BY_NAME(F.CompareByName),
BY_DATE(F.CompareByDate);
private Comparator comparator;
Comparators(final Comparator comparator) {
this.comparator = comparator;
}
public Comparator getComparator() {
return comparator;
}
}
...
class F implements Parcelable {
List<String> comparatorNames = new ArrayList<>();
.... ..
writeToParcel() {
//write list
}
readFromParcel() {
//read list
}
}
然后你想要的任何地方:
f = (F)intent.getParcelableExtra("F");
Comparator c = Comparators.valueOf(f.getComparatorName(0));