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 を使用して別のカテゴリ名のリストを作成できます。これは次のようになります。この例では、これをリストに置き換えることができる2次元配列が使用されています。

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

あなたはあなた自身を書き留めなければなりませんAdapterBaseAdapterたとえば、を拡張して、次のメソッドをオーバーライドできます。

  • getCount()データセット内のアイテムの数を返します
  • getView()それはあなたの視野を膨らませます
  • getItemViewType()getView()(この例ではグループ要素Baroque / Classic)または「通常の」アイテムによって作成されるビューのタイプを取得します。
  • getViewTypeCount ()あなたが持っているビューのタイプの数。
于 2012-10-30T08:49:18.800 に答える
0

私は私の質問に解決策を得ました.Thanks 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 に答える
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 に答える