How to sort & Group in Android?
Posted
by
crickpatel0024
on Stack Overflow
See other posts from Stack Overflow
or by crickpatel0024
Published on 2012-10-30T08:41:51Z
Indexed on
2012/10/31
5:02 UTC
Read the original article
Hit count: 195
I have ArrayList and I want to sort and group all data by header in Android. How it is possible in Android? please help me.below me from owner And set header Me And Joe Manager From owner And set Header in listview. How to do that in Android?
My code in below::
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;
}
}
}
© Stack Overflow or respective owner