How To Add SimpleAdapter In a ListView :     How To Add SimpleAdapter In a Listview With Checkbox.i am using arrayadaptr how to   use simpleadapter throw arrayadapter.how can i use simple adapter in a my listview .How To Add SimpleAdapter In a Listview With Checkbox.i am using arrayadaptr how to   use simpleadapter throw arrayadapter.how can i use simple adapter in a my listview .

             
   
                    
                    import android.app.Activity;
                    import android.app.ProgressDialog;
                    import android.content.Context;
                    import android.content.SharedPreferences;
                    import android.os.AsyncTask;
                    import android.os.Bundle;
                    import android.util.SparseBooleanArray;
                    import android.view.View;
                    import android.widget.AdapterView;
                    import android.widget.ArrayAdapter;
                    import android.widget.Button;
                    import android.widget.ListView;
                    import android.widget.Spinner;
                    import android.widget.Toast;
                    
                    import org.json.JSONArray;
                    import org.json.JSONException;
                    import org.json.JSONObject;
                    
                    import java.io.BufferedReader;
                    import java.io.IOException;
                    import java.io.InputStream;
                    import java.io.InputStreamReader;
                    import java.net.HttpURLConnection;
                    import java.net.MalformedURLException;
                    import java.net.URL;
                    import java.util.ArrayList;
                    import java.util.Arrays;
                    import java.util.HashMap;
                    import java.util.List;
                    import java.util.Map;
                    
                    public class main extends Activity {
                        ListView myList;
                        Button getChoice, clearAll;
                        SharedPreferences sharedpreferences;
                        public static final String MyPREFERENCES = "MyUserChoice";
                        ArrayList<String> selectedItems = new ArrayList<String>();
                        String json_string;
                        String JSON_STRING;
                        JSONObject JO;
                        JSONObject jsonObject;
                        JSONArray jsonArray;
                        int count = 0;
                        ArrayList<String> list = new ArrayList<String>();
                        ArrayList<String> list2 = new ArrayList<String>();
                        ArrayAdapter<String> adapter;
                        Spinner current,new_class;
                        String cur,nc;
                        List<Map<String, String>> data4 = new ArrayList<Map<String, String>>();
                        ProgressDialog pDialog;
                    
                    
                        @Override
                        protected void onCreate(Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                            //new current().execute();
                            new current2().execute();
                            new new_class().execute();
                            current = (Spinner) findViewById(R.id.current_class);
                            new_class = (Spinner)findViewById(R.id.new_class);
                            myList = (ListView) findViewById(R.id.list);
                            getChoice = (Button) findViewById(R.id.getchoice);
                            clearAll = (Button) findViewById(R.id.clearall);
                    
                    
                    
                            current.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    
                                    cur = current.getSelectedItem().toString();
                                    HttpWebCall(cur);
                                    //data4.clear();
                                    list.clear();
                                    list2.clear();
                                }
                    
                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {
                    
                                }
                            });
                    
                            new_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    nc = new_class.getSelectedItem().toString();
                                }
                    
                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {
                    
                                }
                            });
                    
                            getChoice.setOnClickListener(new Button.OnClickListener(){
                    
                    
                                @Override
                                public void onClick(View v) {
                    
                                    String selected = "";
                                    int cntChoice = myList.getCount();
                    
                                    SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
                                    for(int i = 0; i < cntChoice; i++){
                                        if(sparseBooleanArray.get(i)) {
                                            selected += myList.getItemAtPosition(i).toString() + "\n";
                                            System.out.println("Checking list while adding:" + myList.getItemAtPosition(i).toString());
                                            SaveSelections();
                                        }
                    
                                    }
                    
                                    Toast.makeText(main.this, selected, Toast.LENGTH_LONG).show();
                    
                                }});
                    
                            clearAll.setOnClickListener(new View.OnClickListener() {
                    
                                @Override
                                public void onClick(View v) {
                    // TODO Auto-generated method stub
                                    ClearSelections();
                                }
                            });
                    
                        }
                    
                        private void SaveSelections() {
                    // save the selections in the shared preference in private mode for the user
                    
                            SharedPreferences.Editor prefEditor = sharedpreferences.edit();
                            String savedItems = getSavedItems();
                            prefEditor.putString(MyPREFERENCES.toString(), savedItems);
                            prefEditor.commit();
                        }
                    
                        private String getSavedItems() {
                            String savedItems = "";
                            int count = this.myList.getAdapter().getCount();
                            for (int i = 0; i < count; i++) {
                                if (this.myList.isItemChecked(i)) {
                                    if (savedItems.length() > 0) {
                                        savedItems += "," + this.myList.getItemAtPosition(i);
                                    } else {
                                        savedItems += this.myList.getItemAtPosition(i);
                                    }
                                }
                            }
                            return savedItems;
                        }
                    
                    
                    
                        private void ClearSelections() {
                    // user has clicked clear button so uncheck all the items
                            int count = this.myList.getAdapter().getCount();
                            for (int i = 0; i < count; i++) {
                                this.myList.setItemChecked(i, false);
                            }
                    // also clear the saved selections
                            SaveSelections();
                        }
                    
                    
                    
                        class current2 extends AsyncTask<String, String, String> {
                    
                            ProgressDialog pdLoading = new ProgressDialog(main.this);
                            String json_url;
                    
                            @Override
                            protected void onPreExecute() {
                                json_url = "http://192.168.1.10:8081/sms/GetClass.php";
                                pdLoading.setMessage("\tLoading...");
                                pdLoading.isIndeterminate();
                                pdLoading.setIndeterminate(true);
                                pdLoading.setCancelable(false);
                                pdLoading.show();
                            }
                    
                            @Override
                            protected String doInBackground(String... params) {
                                try {
                                    URL url = new URL(json_url);
                                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                                    InputStream inputStream = httpURLConnection.getInputStream();
                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                                    StringBuilder stringBuilder = new StringBuilder();
                    
                                    while ((JSON_STRING = bufferedReader.readLine()).equals("null")) ;
                                    {
                                        stringBuilder.append(JSON_STRING + "\n");
                                    }
                                    bufferedReader.close();
                                    inputStream.close();
                                    httpURLConnection.disconnect();
                                    return stringBuilder.toString().trim();
                    
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }
                    
                            @Override
                            protected void onPostExecute(String result) {
                                List<Map<String, String>> data = null;
                                data = new ArrayList<Map<String, String>>();
                                json_string = result;
                                try {
                                    jsonObject = new JSONObject(json_string);
                                    jsonArray = jsonObject.getJSONArray("server_response");
                                    if (jsonArray.length() != 0) {
                                        String[] s_name = new String[jsonArray.length()];
                                        for (count = 0; count < jsonArray.length(); count++) {
                                            JO = jsonArray.getJSONObject(count);
                                            s_name[count] = JO.getString("Class");
                                        }
                                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(main.this, R.layout.spinner_values,s_name);
                                        current.setAdapter(adapter);
                                    }
                    
                    
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                    
                                pdLoading.dismiss();
                            }
                        }
                    
                        class new_class extends AsyncTask<String, String, String> {
                    
                            ProgressDialog pdLoading = new ProgressDialog(main.this);
                            String json_url;
                    
                            @Override
                            protected void onPreExecute() {
                                json_url = "http://192.168.1.10:8081/sms/GetClass.php";
                                pdLoading.setMessage("\tLoading...");
                                pdLoading.isIndeterminate();
                                pdLoading.setIndeterminate(true);
                                pdLoading.setCancelable(false);
                                pdLoading.show();
                            }
                    
                            @Override
                            protected String doInBackground(String... params) {
                                try {
                                    URL url = new URL(json_url);
                                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                                    InputStream inputStream = httpURLConnection.getInputStream();
                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                                    StringBuilder stringBuilder = new StringBuilder();
                    
                                    while ((JSON_STRING = bufferedReader.readLine()).equals("null")) ;
                                    {
                                        stringBuilder.append(JSON_STRING + "\n");
                                    }
                                    bufferedReader.close();
                                    inputStream.close();
                                    httpURLConnection.disconnect();
                                    return stringBuilder.toString().trim();
                    
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }
                    
                            @Override
                            protected void onPostExecute(String result) {
                                List<Map<String, String>> data = null;
                                data = new ArrayList<Map<String, String>>();
                                json_string = result;
                                try {
                                    jsonObject = new JSONObject(json_string);
                                    jsonArray = jsonObject.getJSONArray("server_response");
                                    if (jsonArray.length() != 0) {
                                        String[] s_name = new String[jsonArray.length()];
                                        for (count = 0; count < jsonArray.length(); count++) {
                                            JO = jsonArray.getJSONObject(count);
                                            s_name[count] = JO.getString("Class");
                                        }
                                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(main.this, R.layout.spinner_values,s_name);
                                        new_class.setAdapter(adapter);
                                    }
                    
                    
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                    
                                pdLoading.dismiss();
                            }
                        }
                    
                        HttpParse httpParse = new HttpParse();
                        String HttpURL = "http://192.168.1.10:8081/sms/select_class.php";
                        String ParseResult;
                        String FinalJSonObject;
                        HashMap<String, String> ResultHash = new HashMap<>();
                    
                        public void HttpWebCall(final String PreviousListViewClickedItem) {
                    
                    
                            class HttpWebCallFunction extends AsyncTask<String, Void, String> {
                    
                                @Override
                                protected void onPreExecute() {
                                    super.onPreExecute();
                    
                                    pDialog = ProgressDialog.show(main.this, "Loading Data", null, true, true);
                                }
                    
                                @Override
                                protected void onPostExecute(String httpResponseMsg) {
                    
                                    super.onPostExecute(httpResponseMsg);
                    
                                    pDialog.dismiss();
                    
                                    //Storing Complete JSon Object into String Variable.
                                    FinalJSonObject = httpResponseMsg;
                    
                                    //Parsing the Stored JSOn String to GetHttpResponse Method.
                                    new GetHttpResponse(main.this).execute();
                    
                                }
                    
                                @Override
                                protected String doInBackground(String... params) {
                    
                                    ResultHash.put("Class", params[0]);
                    
                                    ParseResult = httpParse.postRequest(ResultHash, HttpURL);
                    
                                    return ParseResult;
                                }
                            }
                    
                            HttpWebCallFunction httpWebCallFunction = new HttpWebCallFunction();
                    
                            httpWebCallFunction.execute(PreviousListViewClickedItem);
                        }
                        private class GetHttpResponse extends AsyncTask<Void, Void, Void> {
                    
                            public Context context;
                    
                            public GetHttpResponse(Context context) {
                                this.context = context;
                            }
                    
                            @Override
                            protected void onPreExecute() {
                                super.onPreExecute();
                                pDialog = ProgressDialog.show(main.this, "Loading Data", null, true, true);
                            }
                    
                            @Override
                            protected Void doInBackground(Void... arg0) {
                                try {
                                    if (FinalJSonObject != null) {
                                        JSONArray jsonArray = null;
                    
                                        try {
                    
                                            jsonArray = new JSONArray(FinalJSonObject);
                    
                                            JSONObject jsonObject;
                    
                                            for (int i = 0; i < jsonArray.length(); i++) {
                                                jsonObject = jsonArray.getJSONObject(i);
                    
                                                list.add(jsonObject.getString("Student_Name"));
                                                list2.add(jsonObject.getString("Student_Roll_No"));
                                            }
                                        } catch (JSONException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                return null;
                            }
                    
                            @Override
                            protected void onPostExecute(Void result) {
                                pDialog.dismiss();
                    
                    
                                adapter = new ArrayAdapter<String>(main.this, android.R.layout.simple_list_item_multiple_choice, list);
                    
                    
                                myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                                myList.setAdapter(adapter);
                    
                                sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
                                if(sharedpreferences.contains(MyPREFERENCES)){
                                    if (sharedpreferences.contains(MyPREFERENCES.toString())) {
                    
                                        String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
                                        selectedItems.addAll(Arrays.asList(savedItems.split(",")));
                    
                                        int count = main.this.myList.getAdapter().getCount();
                                        for (int i = 0; i < count; i++) {
                                            String currentItem = (String) myList.getAdapter().getItem(i);
                                            if (selectedItems.contains(currentItem)) {
                                                myList.setItemChecked(i, true);
                    
                                                Toast.makeText(getApplicationContext(),
                                                        "Curren Item: " + currentItem,
                                                        Toast.LENGTH_LONG).show();
                                            } else {
                                                myList.setItemChecked(i, false);
                                            }
                    
                                        }
                                    }
                                }
                    
                            }
                        }
                    
                    }
             
How To Add SimpleAdapter In a Listview With Checkbox.i am using arrayadaptr how to   use simpleadapter throw arrayadapter.how can i use simple adapter in a my listview .How To Add SimpleAdapter In a Listview With Checkbox.i am using arrayadaptr how to   use simpleadapter throw arrayadapter.how can i use simple adapter in a my listview .