在我的应用程序中,我使用自定义适配器ans显示列表视图的服务数据。每件事对我来说都很好,但我想根据“票价”条件对列表视图项进行排序。
任何人都可以帮助我根据“票价”(从最低到最高)对列表视图进行排序
我的代码:
BusData.java:
package com.reloadapp.reload.data;
/**
* Created by user1 on 5/21/2015.
*/
public class BusData
{
private String routescheduleid;
private String companyname;
private String companyid;
private String deptime;
private String arrtime;
private int fare;
private String buslabel;
private String avaliableseats;
private String bustypename;
private String difftime;
boolean ac,non_ac,sleeper,non_slepeer;
private String pickuproutescheduleid;
private String pickupname;
public String getDropoffname() {
return Dropoffname;
}
public void setDropoffname(String dropoffname) {
Dropoffname = dropoffname;
}
public boolean getac() {
return ac;
}
public void setac(boolean ac) {
this.ac = ac;
}
public boolean getnon_ac() {
return non_ac;
}
public void setnon_ac(boolean non_ac) {
this.non_ac = non_ac;
}
public boolean getsleeper() {
return sleeper;
}
public void setsleeper(boolean sleeper) {
this.sleeper = sleeper;
}
public boolean getnon_slepeer() {
return non_slepeer;
}
public void setnon_slepeer(boolean non_slepeer) {
this.non_slepeer = non_slepeer;
}
private String Dropoffname;
public BusData()
{
}
public String getPickuproutescheduleid() {
return pickuproutescheduleid;
}
public void setPickuproutescheduleid(String pickuproutescheduleid) {
this.pickuproutescheduleid = pickuproutescheduleid;
}
public String getPickupname() {
return pickupname;
}
public void setPickupname(String pickupname) {
this.pickupname = pickupname;
}
public String getDifftime() {
return difftime;
}
public void setDifftime(String difftime) {
this.difftime = difftime;
}
public BusData(String routescheduleid,String companyname,String companyid,String deptime,String arrtime,int fare,String buslabel,String avaliableseats,String bustypename,String difftime)
{
this.routescheduleid=routescheduleid;
this.companyname=companyname;
this.companyid=companyid;
this.deptime=deptime;
this.arrtime=arrtime;
this.fare=fare;
this.buslabel=buslabel;
this.avaliableseats=avaliableseats;
this.bustypename=bustypename;
this.difftime=difftime;
}
public String getRoutescheduleid() {
return routescheduleid;
}
public void setRoutescheduleid(String routescheduleid) {
this.routescheduleid = routescheduleid;
}
public String getCompanyname() {
return companyname;
}
public void setCompanyname(String companyname) {
this.companyname = companyname;
}
public String getCompanyid() {
return companyid;
}
public void setCompanyid(String companyid) {
this.companyid = companyid;
}
public String getDeptime() {
return deptime;
}
public void setDeptime(String deptime) {
this.deptime = deptime;
}
public String getArrtime() {
return arrtime;
}
public void setArrtime(String arrtime) {
this.arrtime = arrtime;
}
public int getFare() {
return fare;
}
public void setFare(int fare) {
this.fare = fare;
}
public String getBuslabel() {
return buslabel;
}
public void setBuslabel(String buslabel) {
this.buslabel = buslabel;
}
public String getAvaliableseats() {
return avaliableseats;
}
public void setAvaliableseats(String avaliableseats) {
this.avaliableseats = avaliableseats;
}
public String getBustypename() {
return bustypename;
}
public void setBustypename(String bustypename) {
this.bustypename = bustypename;
}
}
BusAdapter.java:
public class BusDataAdapter extends BaseAdapter {
ArrayList<BusData> bpData;
private ArrayList<BusData> arraylist;
private Activity activity;
ProgressDialog mProgressDialog;
public static String journey_bus;
public static String rid;
String travelname, travetime = null;
private LayoutInflater inflater;
//SearchActivity obj=new SearchActivity(journey_bus);
public BusDataAdapter(Activity activity, ArrayList<BusData> bpData) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.bpData = bpData;
mProgressDialog = new ProgressDialog(activity);
inflater = LayoutInflater.from(activity);
}
public void setJourneyBus(String journey_bus) {
this.journey_bus = journey_bus;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return bpData.size();
}
@Override
public Object getItem(int location) {
// TODO Auto-generated method stub
return location;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.buslistviewitems, null);
Typeface custom_bold = Typeface.createFromAsset(activity.getAssets(),
"fonts/SourceSansPro_Semibold.ttf");
Typeface custom_regular = Typeface.createFromAsset(activity.getAssets(),
"fonts/SourceSansPro_Light.ttf");
viewHolder.deptime = (TextView) convertView
.findViewById(R.id.deptime);
viewHolder.difftime = (TextView) convertView
.findViewById(R.id.difftime);
viewHolder.busfare = (TextView) convertView
.findViewById(R.id.busfare);
viewHolder.buslabel = (TextView) convertView
.findViewById(R.id.buslabel);
viewHolder.routeid = (TextView) convertView
.findViewById(R.id.routeid);
viewHolder.bustypename = (TextView) convertView
.findViewById(R.id.bustypename);
viewHolder.availableseat = (TextView) convertView
.findViewById(R.id.avaliableseats);
viewHolder.businfo = (LinearLayout) convertView.findViewById(R.id.businfo);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.deptime.setText(bpData.get(position).getDeptime() + "-" + bpData.get(position).getArrtime());
// travetime = bpData.get(position).getDeptime() + "-" + bpData.get(position).getArrtime();
viewHolder.difftime.setText(bpData.get(position).getDifftime());
viewHolder.busfare.setText(bpData.get(position).getFare());
viewHolder.buslabel.setText(bpData.get(position).getBuslabel());
viewHolder.routeid.setText(bpData.get(position).getRoutescheduleid());
viewHolder.bustypename.setText(bpData.get(position).getBustypename());
// travelname = bpData.get(position).getBuslabel();
viewHolder.availableseat.setText(bpData.get(position).getAvaliableseats() + "Seats");
if (bpData.get(position).getAvaliableseats().equalsIgnoreCase("0")) {
viewHolder.availableseat.setText("Sold Out");
} else {
viewHolder.availableseat.setText(bpData.get(position).getAvaliableseats() + "Seats");
}
class ViewHolder {
TextView difftime, deptime, busfare, buslabel, routeid, bustypename, availableseat;
LinearLayout businfo;
//Button businfo;
}}
FromtoActivity.java:
public class FromtoActivity extends AppCompatActivity {
ArrayList<BusData> bdata = new ArrayList<BusData>();
ArrayList<BusData> filter_list = new ArrayList<BusData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fromto);
Intent intent = getIntent();
json_object = intent.getStringExtra("json_objcet");
busdata();
}
public void busdata() {
try {
bdata.clear();
travel_list.clear();
filter_list.clear();
JSONObject result = new JSONObject(json_object);
JSONObject Data = result.getJSONObject("Data");
routearray = Data.getJSONArray("Route");
for (int i = 0; i < routearray.length(); i++) {
// String companyid = routearray.getJSONObject(i).getString("CompanyId");
CompanyName = routearray.getJSONObject(i).getString("CompanyName");
String deptime = routearray.getJSONObject(i).getString("DepTime");
routeScheduleId = routearray.getJSONObject(i).getString("RouteScheduleId");
String arrtime = routearray.getJSONObject(i).getString("ArrTime");
/* String dtDeparture = "2014-12-15T13:30:00";
String dtArrival = "2014-12-15T23:00:00";*/
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date dateDeparture = format.parse(deptime);
Date dateArrival = format.parse(arrtime);
dateArrival.compareTo(dateDeparture);
long diff = dateArrival.getTime() - dateDeparture.getTime();
long arrivaltime = dateArrival.getTime();
arrivaldate = new SimpleDateFormat("hh:mm a").format(new Date(arrivaltime));
long departuretime = dateDeparture.getTime();
depardate = new SimpleDateFormat("hh:mm a").format(new Date(departuretime));
/* Log.v("TAG_realarrtime",""+arrivaldate);
Log.v("TAG_realdeptime",""+depardate);
*/
long hours = TimeUnit.MILLISECONDS.toHours(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff) - hours * 60;
// System.out.println(hours + "." + minutes + "hrs");
msg = hours + "." + minutes + "hrs";
fare = routearray.getJSONObject(i).getInt("Fare");
hasac = routearray.getJSONObject(i).getString("HasAC");
hasnac = routearray.getJSONObject(i).getString("HasNAC");
hasseater = routearray.getJSONObject(i).getString("HasSeater");
hassleeper = routearray.getJSONObject(i).getString("HasSleeper");
String isvolvo = routearray.getJSONObject(i).getString("IsVolvo");
buslabel = routearray.getJSONObject(i).getString("BusLabel");
avaliableseats = routearray.getJSONObject(i).getString("AvailableSeats");
bustypename = routearray.getJSONObject(i).getString("BusTypeName");
BusData bs = new BusData();
// bs.setCompanyname(CompanyName);
//bs.setCompanyid(companyid);
bs.setFare(fare);
bs.setBuslabel(CompanyName);
bs.setBustypename(bustypename);
bs.setAvaliableseats(avaliableseats);
bs.setArrtime(arrivaldate);
bs.setDeptime(depardate);
bs.setDifftime(msg);
bs.setac(routearray.getJSONObject(i).getBoolean("HasAC"));
bs.setnon_ac(routearray.getJSONObject(i).getBoolean("HasNAC"));
bs.setsleeper(routearray.getJSONObject(i).getBoolean("HasSleeper"));
bs.setnon_slepeer(routearray.getJSONObject(i).getBoolean("HasSeater"));
bs.setRoutescheduleid(routeScheduleId);
bdata.add(bs);
if (!travel_list.contains(bdata.get(i).getBuslabel()))
travel_list.add(bdata.get(i).getBuslabel());
}
adapter = new BusDataAdapter(this, bdata);
fromto.setAdapter(adapter);
adapter.setJourneyBus(date_bus);
filter_list.addAll(bdata);
adapter = new BusDataAdapter(this, filter_list);
fromto.setAdapter(adapter);
/* adapter.setrouteid(routeScheduleId);
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
您应该在调用适配器之前对列表进行排序以列出数据。
Collections.sort(bdata, new Comparator<BusData>() {
public int compare(BusData o1, BusData o2) {
return o2.getFare().compareTo(o1.getFare());
}
});
for(BusData busData:bdata)
{
Log.e("Fare : ",busData.getFare());
}
排序完成后调用适配器列出数据:
adapter = new BusDataAdapter(this, bdata);
fromto.setAdapter(adapter);
adapter.setJourneyBus(date_bus);
filter_list.addAll(bdata);
adapter = new BusDataAdapter(this, filter_list);
fromto.setAdapter(adapter);
答案 1 :(得分:0)
您可以使用Collections.sort方法。
这样做......
Collections.sort(filterlist, new Comparator<bData>() {
@Override
public int compare(bData lhs, bData rhs) {
return (lhs.getFair() > rhs.getFair()) ? -1 : (lhs.getFair() > rhs.getFair()) ? 1 : 0;
}
});
答案 2 :(得分:0)
你需要在android中使用比较器api。
Comparator<BusData> comparator=new Comparator<BusData>() {
@Override
public int compare(final BusData lhs, final BusData rhs) {
return lhs.getFare().equals(rhs.getFare());
}
};
Collections.sort(busDataList,comparator);
然后您的列表将被排序。
答案 3 :(得分:0)
您需要将已排序的总线集合提供给适配器。适配器也不应该负责排序和ListView。 试试这个:
Collections.sort(bdata, new Comparator<BusData>() {
@Override
public int compare(BusData lhs, BusData rhs) {
return lhs.getFare() > rhs.getFare();
}
});
答案 4 :(得分:0)
您可以使用比较器并对列表进行排序
class FareComparator implements Comparator{
public int Compare(Object o1,Object o2){
BusData s1=(BusData)o1;
BusData s2=(BusData)o2;
if(s1.fare==s2.fare)
return 0;
else if(s1.fare>s2.fare)
return -1;
else
return 1;
}
}
并使用
Collections.sort(bdata,new FareComparator());
之前的
adapter = new BusDataAdapter(this, bdata);