排序非NULL日期升序或降序和null日期最后显示在android的listview中?

时间:2012-11-05 10:56:25

标签: android sorting arraylist

我有arraylist和所有数据nulldate和日期添加在一个数组列表中。我在列表视图中显示所有数据。但我的问题是我先得到nulldate和最后的可用日期我想在可用日期之前使日期数据为空。我想排序可用日期和空日期数据没有排序,只是在add.please列表视图帮助我.... !!!!!!!!!!!

public class Approval extends Activity {
    protected static final All_Approval_data_dto All_Approval_data_dto = null;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        list.clear();
    }

    private String assosiatetoken;
    ArrayList<All_Approval_data_dto> list = new ArrayList<All_Approval_data_dto>();
    ArrayList<All_Approval_Key_dto> alist = new ArrayList<All_Approval_Key_dto>();
    ArrayList<String> catState = new ArrayList<String>();
    ArrayList<String> uniCatState = new ArrayList<String>();
    ArrayList<ArrayList<All_Approval_data_dto>> masterState = new ArrayList<ArrayList<All_Approval_data_dto>>();
    private String req_id;
    ListView lv;
    Button back;
    ExpAdapter1 expAdapter;
    private Spinner spndata;
    String[] reqspinner = { "Request Date", "Last Update", "Submitter",
            "Owner", "State" };
    ArrayAdapter<String> adapter;
    private ExpandableListView exlistView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.approval);
        assosiatetoken = MyApplication.getToken();
        alist = DBAdpter.recursUserData(assosiatetoken);
        for (int i = 0; i < alist.size(); i++) {
            req_id = alist.get(i).requestId;
            list = DBAdpter.approvalUserData(assosiatetoken, req_id);

        }

        // setState();
        new doinbackground(this).execute();
        spndata = (Spinner) findViewById(R.id.list_all_quize);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, reqspinner);
        spndata.setAdapter(adapter);
        lv = (ListView) findViewById(R.id.listapprovaldata);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                Intent edit = new Intent(Approval.this, Approval_webview.class);
                // edit.putExtra("Cat_url", url_link);

                startActivity(edit);
            }
        });
        exlistView = (ExpandableListView) findViewById(R.id.ExpList);
        spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {
                case 0:
                    lv.setVisibility(View.VISIBLE);
                    exlistView.setVisibility(View.GONE);
                    Collections.sort(list, byDate);
                    for (int i = 0; i < list.size(); i++) {
                        Log.v("log_tag", "Data " + list.get(i).lastModifiedDate);
                        if (list.get(i).lastModifiedDate != null) {

                            Log.v("log_tag", "sorting "
                                    + list.get(i).lastModifiedDate);
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));

                        }

                    }

                    break;
                case 1:
                    lv.setVisibility(View.VISIBLE);
                    exlistView.setVisibility(View.GONE);
                    Collections.sort(list, byDate1);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).state != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 2:

                    break;

                case 3:

                    break;

                default:
                    break;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

        });

    }

    static final Comparator<All_Approval_data_dto> byDate = new Comparator<All_Approval_data_dto>() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

        @SuppressWarnings("unused")
        public int compare(All_Approval_data_dto ord1,
                All_Approval_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {

                if (d1 == null) {
                    return (d2 == null) ? 0 : -1;
                } else if (d2 == null) {
                    return 1;
                }
                d1 = sdf.parse(ord1.lastModifiedDate);
                d2 = sdf.parse(ord2.lastModifiedDate);
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block`
                e.printStackTrace();
            }
            // return (d1.compareTo(d2) > 0 ? 1 : -1);
            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
        }

    };

    /*
     * static final Comparator<AnalysisDto> byRatio = new
     * Comparator<AnalysisDto>() {
     * 
     * 
     * public int compare(Date date1, Date date2) { return
     * (date1.compareTo(date2)>0 ? 1 : -1); } };
     */
    static final Comparator<All_Approval_data_dto> byDate1 = new Comparator<All_Approval_data_dto>() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

        @SuppressWarnings("unused")
        public int compare(All_Approval_data_dto ord1,
                All_Approval_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {

                if (d1 == null) {
                    return (d2 == null) ? 0 : -1;
                } else if (d2 == null) {
                    return 1;
                }
                d1 = sdf.parse(ord1.submitDate);
                d2 = sdf.parse(ord2.submitDate);

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block`
                e.printStackTrace();
            }
            // return (d1.compareTo(d2) > 0 ? 1 : -1);
            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }
    };

    class doinbackground extends AsyncTask<Void, Void, Void> {
        ProgressDialog pd;
        private Context ctx;

        public doinbackground(Context c) {
            ctx = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(ctx);
            pd.setMessage("Loading...");
            pd.show();

        }

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            pd.cancel();

        }

    }

    public class MyListAdapter extends BaseAdapter {
        private ArrayList<All_Approval_data_dto> list;

        public MyListAdapter(Context mContext,
                ArrayList<All_Approval_data_dto> list) {
            this.list = list;
        }

        public int getCount() {
            return list.size();
        }

        public All_Approval_data_dto getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.custom_approval_data, null);
            TextView req_id = (TextView) convertView.findViewById(R.id.req_txt);
            TextView date = (TextView) convertView.findViewById(R.id.date_txt);
            TextView owner = (TextView) convertView
                    .findViewById(R.id.owner_txt);
            TextView state = (TextView) convertView
                    .findViewById(R.id.state_txt);
            req_id.setText(list.get(position).requestId + " - "
                    + list.get(position).title);
            date.setText(list.get(position).lastModifiedDate + " - "
                    + list.get(position).submitDate);
            owner.setText(list.get(position).submitter);
            state.setText(list.get(position).state);
            return convertView;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用您的数据并按升序或降序进行管理

Java Array Sort descending?

答案 1 :(得分:0)

用这个

替换你的try / catch块
 try {

            if (ord1 == null) {
                return (ord2 == null) ? 0 : 1;
            } else if (ord2 == null) {
                return -1;
            }
            d1 = sdf.parse(ord1.lastModifiedDate);
            d2 = sdf.parse(ord2.lastModifiedDate);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block`
            e.printStackTrace();
        }