Define a custom interface that is set to callback from the multiple-element selection dialog box.
public interface MultipleItemSelectListener { public void onSelected(String value,String ids); }
Special dialog for selecting multiple items.
Options
Content : a link to your content.
title : title of the dialogue.
jsonArrayString : your jsonarray response as a string.
MultipleItemSelectListener : user element of the selected listener.
public void getMultiSelectionDialog(final Context context,final String title, String jsonString, final String value, final MultipleItemSelectListener target) { final ArrayList<String> values = new ArrayList<String>(); final ArrayList<String> ids = new ArrayList<String>(); final String BUSINESS_ID="id"; final String BUSINESS_NAME="name"; try { JSONArray jsonArray = new JSONArray(jsonString); int size = jsonArray.length(); for (int i = 0; i < size; i++) { values.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_NAME).trim()); ids.add(((JSONObject) jsonArray.get(i)).getString(BUSINESS_ID).trim()); } } catch (JSONException e) { e.printStackTrace(); } final boolean[] selections = new boolean[values.size()]; final StringBuilder newValue = new StringBuilder(); final StringBuilder newIds = new StringBuilder(); AlertDialog alert = null; if (value.length() > 0) { String[] oldValue = value.split(","); int size = values.size(); for (int i = 0; i < size; i++) { int len = oldValue.length; for (int j = 0; j < len; j++) { if (values.get(i).trim().equalsIgnoreCase(oldValue[j].trim())) { selections[i] = true; break; } } } } AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setMultiChoiceItems(values.toArray(new CharSequence[values.size()]), selections, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { selections[which] = isChecked; } }); builder.setPositiveButton("Done", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int size = selections.length; for (int i = 0; i < size; i++) { if (selections[i]) { newValue.append(newValue.length() > 0 ? "," + values.get(i) : values.get(i)); newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i)); } } target.onSelected(newValue.toString(),newIds.toString()); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); int size = selections.length; for (int i = 0; i < size; i++) { if (selections[i]) { newIds.append(newIds.length() > 0 ? "," + ids.get(i) : ids.get(i)); } } target.onSelected(value,newIds.toString()); } }); alert = builder.create(); alert.show(); }
How to get a MultiiSelectionDialog:
class LoadBusiness extends AsyncTask<Void,Void,String> { Context context; public LoadBusiness(Context context){ this.context=context; } private ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(context); pDialog.setMessage("Loading..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected String doInBackground(Void... params) { ServiceHandler sh = new ServiceHandler(); String jsonStr = sh.makeServiceCall(BUSINESS_URL, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); return jsonStr; } protected void onPostExecute(String result) { super.onPostExecute(result); pDialog.dismiss(); setTextViewWithMultipleSelectionDialog(context,result); } } public void setTextViewWithMultipleSelectionDialog(final Context context,final String jsonArrayString){ splang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getMultiSelectionDialog(context,"Countries",jsonArrayString,splang.getText().toString(),new MultipleItemSelectListener() { @Override public void onSelected(String value) { splang.setText(value); } }); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); splang=(TextView)findViewById(R.id.txtvw); new LoadBusiness(this).execute(); }