1

我有 ArrayList,我想在 Android 中按标题对所有数据进行排序和分组。在Android中怎么可能?请帮助我。在我下面从所有者那里设置标题我和乔经理从所有者那里并在列表视图中设置标题。如何在 Android 中做到这一点?

在此处输入图像描述

我的代码在下面::

public class Request extends Activity {
    private String assosiatetoken;
    private ArrayList<All_Request_data_dto> list = new ArrayList<All_Request_data_dto>();
    ListView lv;
    Button back;
    private Spinner spndata;
    String[] reqspinner = { "Request Date", "Last Update", "Type", "Owner",
            "State" };
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.request);
        assosiatetoken = MyApplication.getToken();

        new doinbackground(this).execute();

        back = (Button) findViewById(R.id.button1);
        spndata = (Spinner) findViewById(R.id.list_all_quize_req);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, reqspinner);
        spndata.setAdapter(adapter);
        lv = (ListView) findViewById(R.id.listrequestdata);
        lv.setOnItemClickListener(new OnItemClickListener() {

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

                startActivity(edit);
            }
        });

        spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {

                case 0:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    Collections.sort(list, byDate1);
                    // Collections.reverse(list);
                    for (int i = 0; i < list.size(); i++) {

                        if (list.get(i).submitDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 1:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    Collections.sort(list, byDate);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).lastModifiedDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 2:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    Collections.sort(list, byDate3);
                    // Collections.reverse(list);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).state != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;

                case 3:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    for (int i = 0; i < list.size(); i++) {

                        lv.setAdapter(new MyListAdapter(
                                getApplicationContext(), list));

                    }
                    break;

                default:
                    break;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }

        });
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

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

        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {
                d1 = sdf.parse(ord1.lastModifiedDate);
                d2 = sdf.parse(ord2.lastModifiedDate);

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }
    };
    static final Comparator<All_Request_data_dto> byDate1 = new Comparator<All_Request_data_dto>() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {
                d1 = sdf.parse(ord1.submitDate);
                d2 = sdf.parse(ord2.submitDate);

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }
    };
    static final Comparator<All_Request_data_dto> byDate3 = new Comparator<All_Request_data_dto>() {
        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            String d1 = null;
            String d2 = null;
            d1 = ord1.state;
            d2 = ord2.state;
            return d1.compareToIgnoreCase(d2);
        }
    };

    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_Request_data_dto> list;

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

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

        }

        public All_Request_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) {
            // if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.custom_request_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).owner);
            state.setText(list.get(position).state);

            // }
            return convertView;
        }
    }

}
4

4 回答 4

1

您可以为每个类别制作单独的列表,并为这些列表制作一个列表,并为类别名称制作另一个列表,而不是您可以使用 ExpandableListView 和 Adapter ,如下所示。在此示例中,它使用 2d 数组,您可以将其替换为您的列表。

public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
String[][] arrChildelements;
String[] arrGroup;

public ExpAdapter(Context context, String[] arrGroup, String[][] arrChild) {
    myContext = context;
    this.arrGroup = arrGroup;
    this.arrChildelements = arrChild;
}

public Object getChild(int groupPosition, int childPosition) {
    return null;
}

public long getChildId(int groupPosition, int childPosition) {
    return 0;
}

public int getChildrenCount(int groupPosition) {

    return arrChildelements[groupPosition].length;
}

public Object getGroup(int groupPosition) {
    return null;
}

public int getGroupCount() {
    return arrGroup.length;
}

public long getGroupId(int groupPosition) {
    return 0;
}

public boolean hasStableIds() {
    return false;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
    }

    TextView tvGroupName = (TextView) convertView
            .findViewById(R.id.tvGroupName);
    tvGroupName.setText(arrGroup[groupPosition]);

    return convertView;
}

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_row, null);
    }

    TextView tvPlayerName = (TextView) convertView
            .findViewById(R.id.tvPlayerName);
    tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);

    return convertView;
}
}
于 2012-10-30T12:36:35.633 回答
0

你必须写下你自己的Adapter。例如,您可以扩展BaseAdapter并覆盖以下方法:

  • getCount()它会返回数据集中的项目数
  • getView()它会扩大你的视野
  • getItemViewType()获取将由 getView() (示例中的组元素 Baroque/Classic)或“普通”项创建的视图类型。
  • getViewTypeCount ()您拥有的视图类型的数量。
于 2012-10-30T08:49:18.800 回答
0

我不确定,但如果你试图缩短你的列表,那么试试这个字符串

public void sort(List<arraylist> itemLocationList) {
    if(itemLocationList!=null)
    {
        Collections.sort(itemLocationList, new Comparator<arraylist>() {
            @Override
            public int compare(list o1, list o2) {
                return o1.res_name.compareToIgnoreCase(o2.res_name);
            }           
        });
    }
}
于 2012-10-30T08:53:31.313 回答
0

我的问题得到了解决。谢谢Jignesh!!!

public class Request extends Activity {
    private String assosiatetoken;
    private ArrayList<All_Request_data_dto> list = new ArrayList<All_Request_data_dto>();
    ListView lv;
    ExpandableListView exlistView;
    ArrayList<String> catOwner = new ArrayList<String>();
    ArrayList<String> uniCatOner = new ArrayList<String>();
    ArrayList<ArrayList<All_Request_data_dto>> masterOwner = new ArrayList<ArrayList<All_Request_data_dto>>();
    ArrayList<String> catState = new ArrayList<String>();
    ArrayList<String> uniCatState = new ArrayList<String>();
    ArrayList<ArrayList<All_Request_data_dto>> masterState = new ArrayList<ArrayList<All_Request_data_dto>>();
    Button back;
    private Spinner spndata;
    String[] reqspinner = { "Request Date", "Last Update", "Type", "Owner",
            "State" };

    ArrayAdapter<String> adapter;
    ExpAdapter expAdapter;

    private void setOwner() {
        for (int i = 0; i < list.size(); i++) {
            catOwner.add(list.get(i).owner);
        }

        HashSet<String> hasSetstate = new HashSet<String>(catOwner);
        uniCatOner = new ArrayList<String>(hasSetstate);

        for (String str : uniCatOner) {
            ArrayList<All_Request_data_dto> cats = new ArrayList<All_Request_data_dto>();
            masterOwner.add(cats);
        }

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < uniCatOner.size(); j++) {
                if (uniCatOner.get(j).equals(list.get(i).owner)) {
                    masterOwner.get(j).add(list.get(i));
                }
            }
        }
    }

    private void setState() {
        for (int i = 0; i < list.size(); i++) {
            catState.add(list.get(i).state);
        }

        HashSet<String> hasSetstate = new HashSet<String>(catState);
        uniCatState = new ArrayList<String>(hasSetstate);

        for (String str1 : uniCatState) {
            ArrayList<All_Request_data_dto> cats = new ArrayList<All_Request_data_dto>();
            masterState.add(cats);
        }

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < uniCatState.size(); j++) {
                if (uniCatState.get(j).equals(list.get(i).state)) {
                    masterState.get(j).add(list.get(i));
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.request);
        assosiatetoken = MyApplication.getToken();
        list = DBAdpter.requestUserData(assosiatetoken);
        exlistView = (ExpandableListView) findViewById(R.id.ExpList);

        setOwner();
        setState();

        new doinbackground(this).execute();

        back = (Button) findViewById(R.id.button1);
        spndata = (Spinner) findViewById(R.id.list_all_quize_req);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, reqspinner);
        spndata.setAdapter(adapter);
        lv = (ListView) findViewById(R.id.listrequestdata);
        lv.setOnItemClickListener(new OnItemClickListener() {

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

                startActivity(edit);
            }
        });

        spndata.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {

                case 0:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    Collections.sort(list, byDate1);
                    exlistView.setFocusable(false);
                    // Collections.reverse(list);
                    for (int i = 0; i < list.size(); i++) {

                        if (list.get(i).submitDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 1:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    exlistView.setVisibility(View.GONE);
                    Collections.sort(list, byDate);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).lastModifiedDate != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;
                case 2:
                    list = DBAdpter.requestUserData(assosiatetoken);
                    exlistView.setVisibility(View.GONE);
                    Collections.sort(list, byDate3);
                    // Collections.reverse(list);
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).state != null) {
                            lv.setAdapter(new MyListAdapter(
                                    getApplicationContext(), list));
                        }
                    }
                    break;

                case 3:

                    lv.setVisibility(View.GONE);
                    expAdapter = new ExpAdapter(Request.this, uniCatOner,
                            masterOwner);
                    exlistView.setAdapter(expAdapter);
                    break;
                case 4:

                    lv.setVisibility(View.GONE);
                    expAdapter = new ExpAdapter(Request.this, uniCatState,
                            masterState);
                    exlistView.setAdapter(expAdapter);
                    break;

                default:
                    break;
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }

        });
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

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

        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {
                d1 = sdf.parse(ord1.lastModifiedDate);
                d2 = sdf.parse(ord2.lastModifiedDate);

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }
    };
    static final Comparator<All_Request_data_dto> byDate1 = new Comparator<All_Request_data_dto>() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            java.util.Date d1 = null;
            java.util.Date d2 = null;
            try {
                d1 = sdf.parse(ord1.submitDate);
                d2 = sdf.parse(ord2.submitDate);

            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return (d1.getTime() > d2.getTime() ? -1 : 1); // descending
            // return (d1.getTime() > d2.getTime() ? 1 : -1); //ascending
        }
    };
    static final Comparator<All_Request_data_dto> byDate3 = new Comparator<All_Request_data_dto>() {
        public int compare(All_Request_data_dto ord1, All_Request_data_dto ord2) {
            String d1 = null;
            String d2 = null;
            d1 = ord1.state;
            d2 = ord2.state;
            return d1.compareToIgnoreCase(d2);
        }
    };

    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_Request_data_dto> list;

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

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

        }

        public All_Request_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) {
            // if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.custom_request_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).owner);
            state.setText(list.get(position).state);

            // }
            return convertView;
        }
    }

}
于 2012-11-01T05:21:57.160 回答