我正在使用一个带有两个arraylist和一个Adapter的ExpndableListview。 如何按降序对子级和父级arraylist数据进行排序。
1.parent Arraylist
private List<PhoneNumber> listDataHeader = new ArrayList<>();
2.child list
private HashMap<String, List<SubBalance>> listDataChild = new HashMap<>();
我在Activity类中使用Adapter代码如下:
listAdapter = new ExpandableAdapter(getActivity(), listDataHeader, listDataChild, AccountProfileFragment.this);
ExpandableAdapter代码
public class ExpandableAdapter extends BaseExpandableListAdapter implements OnClickListener, OnCheckedChangeListener {
private Context context;
private List<PhoneNumber> _listDataHeader;
private HashMap<String, List<SubBalance>> _listDataChild;
private DataSwitchListner listner;
private Activity activity;
private LoadingDialog loading;
public ExpandableAdapter(Activity activity, List<PhoneNumber> listDataHeader, HashMap<String, List<SubBalance>> listChildData, DataSwitchListner listner) {
loading = LoadingDialog.getInstance();
this.context = activity;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this.listner = listner;
this.activity = activity;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition).getNumber()).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@SuppressLint("InflateParams")
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_exp_child, null);
}
SubBalance account = (SubBalance) getChild(groupPosition, childPosition);
TextView currency = (TextView) convertView.findViewById(R.id.currency);
TextView balance = (TextView) convertView.findViewById(R.id.balance);
TextView expiry = (TextView) convertView.findViewById(R.id.expiry);
TextView days = (TextView) convertView.findViewById(R.id.days);
TextView daysLabel = (TextView) convertView.findViewById(R.id.daysLabel);
TextView lblInfinite = (TextView) convertView.findViewById(R.id.lblInfinite);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
LinearLayout llChart = (LinearLayout) convertView.findViewById(R.id.llChart);
if (account != null && account.getCurrencyName() != null && currency != null) {
currency.setText(account.getCurrencyName());
balance.setText(account.getBalance());
String expDateStr = account.getExpiryDate();
Log.e("expDateStr12345",expDateStr);
Date expDate;
if (expDateStr.contains(".")) {
/* expiry.setText(DateUtils.convertFromOldFormatToNewFormat(expDateStr,
DateUtils.FORMAT_1,
DateUtils.FORMAT_4));*/
expiry.setText(DateUtils.convertFromOldFormatToNewFormat(expDateStr,
DateUtils.FORMAT_1,
DateUtils.FORMAT_1));
expDate = DateUtils.convertFromString(expDateStr, DateUtils.FORMAT_1);
LogEvent.Log("ExpandableAdapterdate1", "Phone: " + expDate);
} else {
expiry.setText(DateUtils.convertFromOldFormatToNewFormat(expDateStr,
DateUtils.FORMAT_1_WOD,
DateUtils.FORMAT_1_WOD));
expDate = DateUtils.convertFromString(expDateStr, DateUtils.FORMAT_1_WOD);
LogEvent.Log("ExpandableAdapterdate", "Phone: " + expDate);
}
long daysBetween = DateUtils.daysBetween(new Date(), expDate);
Log.e("expDateStr12345",""+daysBetween);
int progress = 0;
if (daysBetween > 0) {
progress = 100;
if (daysBetween <= 30) {
progress = (int) ((daysBetween / 30.0) * 100);
}
}
days.setTypeface(Methods.getNovaBoldItalic());
Log.e("expDateStr12345",""+days);
if (daysBetween > 100) {
progressBar.setVisibility(View.VISIBLE);
llChart.setVisibility(View.VISIBLE);
lblInfinite.setVisibility(View.INVISIBLE);
//days.setText("NO");
days.setText("No");
daysLabel.setText("VENCE");
progress = 0;
// Log.e("expDateStr12345",""+days);
} else {
progressBar.setVisibility(View.VISIBLE);
llChart.setVisibility(View.VISIBLE);
lblInfinite.setVisibility(View.INVISIBLE);
days.setText(Long.toString(daysBetween));
// Log.e("expDateStr12345",""+days.getText().toString());
}
progressBar.setProgress(progress);
try {
if (account.getIconResource() != null) {
imgIcon.setImageResource(Integer.parseInt(account.getIconResource()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition).getNumber()).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@SuppressLint("InflateParams")
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
PhoneNumber account = (PhoneNumber) getGroup(groupPosition);
CheckBox cbxIsOn;
FontTextView lblPhoneNumber = null;
TextView lblBalance = null,
lblTarrif = null,
lblBundles = null,
lblReload = null,
lblAddbundle = null,
lblHistory = null;
LinearLayout llHeader = null;
ImageView imgIndicator = null;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_exp_parent1, null);
}
cbxIsOn = (CheckBox) convertView.findViewById(R.id.cbxIsOn);
lblPhoneNumber = (FontTextView) convertView.findViewById(R.id.lblPhoneNumber);
lblBalance = (TextView) convertView.findViewById(R.id.lblBalance);
lblTarrif = (TextView) convertView.findViewById(R.id.lblTarrif);
lblBundles = (TextView) convertView.findViewById(R.id.lblBundles);
imgIndicator = (ImageView) convertView.findViewById(R.id.imgIndicator);
llHeader = (LinearLayout) convertView.findViewById(R.id.llHeader);
lblReload = (TextView) convertView.findViewById(R.id.lblReload);
lblAddbundle = (TextView) convertView.findViewById(R.id.lblAddbundle);
lblHistory = (TextView) convertView.findViewById(R.id.lblHistory);
if (isExpanded)
llHeader.setVisibility(View.VISIBLE);
else
llHeader.setVisibility(View.GONE);
if (account.getIsVerified().equalsIgnoreCase("0")) {
cbxIsOn.setEnabled(false);
lblTarrif.setVisibility(View.INVISIBLE);
lblBundles.setVisibility(View.INVISIBLE);
lblBalance.setVisibility(View.INVISIBLE);
} else {
cbxIsOn.setEnabled(true);
lblTarrif.setVisibility(View.VISIBLE);
lblBundles.setVisibility(View.VISIBLE);
lblBalance.setVisibility(View.VISIBLE);
}
lblReload.setTag(groupPosition);
lblAddbundle.setTag(groupPosition);
lblHistory.setTag(groupPosition);
lblReload.setOnClickListener(this);
lblAddbundle.setOnClickListener(this);
lblHistory.setOnClickListener(this);
if (account != null) {
LogEvent.Log("Expandable Adapter", "Phone: " + account.getNumber());
lblPhoneNumber.setText(account.getNumber());
LogEvent.Log("Expandable Adapter", "Plan: " + account.getTariffName());
lblTarrif.setText(account.getTariffName());
LogEvent.Log("Expandable Adapter", "Bundle: " + account.getBundles());
lblBundles.setText(account.getBundles());
if (account.getCacheBalance() != null) {
lblBalance.setText("S/ " + account.getCacheBalance());
}
imgIndicator.setSelected(isExpanded);
cbxIsOn.setOnCheckedChangeListener(null);
cbxIsOn.setChecked(account.isEnabled());
cbxIsOn.setTag(groupPosition);
cbxIsOn.setOnCheckedChangeListener(this);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public void onClick(final View view) {
final PhoneNumber ph = (PhoneNumber) getGroup((int) view.getTag());
final String phone = ph.getNumber();
LogEvent.Log("Expandable Adapter", "Phone: " + ph.getNumber());
LogEvent.Log("Expandable Adapter", "Phone Id: " + ph.getId());
switch (view.getId()) {
case R.id.lblReload:
SPManager.save(SPManager.KEY_CURRENT_PHONE, phone);
SPManager.save(SPManager.KEY_CURRENT_PHONE_ID, ph.getId());
getBalances(MainActivity.FRAG_RELOAD);
break;
case R.id.lblAddbundle:
SPManager.save(SPManager.KEY_CURRENT_PHONE, phone);
SPManager.save(SPManager.KEY_CURRENT_PHONE_ID, ph.getId());
getBalances(MainActivity.FRAG_PACKAGES);
break;
case R.id.lblHistory:
SPManager.save(SPManager.KEY_CURRENT_PHONE, phone);
SPManager.save(SPManager.KEY_CURRENT_PHONE_ID, ph.getId());
getBalances(MainActivity.FRAG_HISTORY);
break;
default:
break;
}
}
private void getBalances(final int gotoThis) {
String currentPhoneNumber = SPManager.retrive(SPManager.KEY_CURRENT_PHONE);
try {
QueryBuilder<PhoneNumber, Integer> qb = App.getDbHelper().getPhoneNumbersDao().queryBuilder();
qb.where().eq("number", currentPhoneNumber);
PhoneNumber phoneNumber = qb.queryForFirst();
String token = SPManager.retrive(SPManager.KEY_TOKEN);
String subId = SPManager.retrive(SPManager.KEY_CURRENT_PHONE_ID);
MyEndpoint e = new MyEndpoint();
e.setUrl(Urls.BASE_URL_NEW);
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setEndpoint(e);
if (loading != null && !loading.isShowing()) {
loading.showDialog(context);
}
eLog("Balances: " + Urls.BASE_URL_NEW + Urls.METHOD_BALANCES);
// eLog("Balances: " + Webservices.balance(subId, token));
eLog("Balances: " + Webservices.balance(subId, token));
builder.build().create(RetroClient.class).balancesWb(Webservices.balance(subId, token),
new Callback<BalancesResponse>() {
@Override
public void success(BalancesResponse m, retrofit.client.Response arg1) {
if (loading != null && loading.isShowing()) {
loading.dismissDialog();
}
if (Strings.RESPONSE_SUCCESS.equalsIgnoreCase(m.getCode())) {
Methods.updateCurrentBalanceCurrency(m.getMain().getBalance(), m.getMain().getCurrency());
eLog("Sliding menu go to: " + gotoThis);
((MainActivity) activity).selectSideMenu(gotoThis, true);
} else {
DialogUtil.displayAlert(context,
context.getString(R.string.dialog_title_error),
Strings.getError(m.getError(), context),
context.getString(R.string.dialog_button_ok));
}
}
@Override
public void failure(RetrofitError e) {
eLog("Error from retrofit: " + e.getMessage());
if (loading != null && loading.isShowing()) {
loading.dismissDialog();
}
}
});
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = (int) buttonView.getTag();
PhoneNumber ph = _listDataHeader.get(pos);
_listDataHeader.get(pos).setEnabled(buttonView.isChecked());
listner.onDataSwitched(isChecked, ph.getNumber(), ph.getSubscriberId());
}
public void eLog(String str) {
Log.e("TAG", "" + str);
}
请帮助我如何按降序排序expandableListview数据 谢谢
答案 0 :(得分:0)
public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
}
};
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}