dimanche 24 mai 2015

How to insert value of multiple checkbox in mysql database in android?

i have a list of textview and checkbox. there is a spinner in the activity. according to spinner data the is fetched form the database. when the list is displayed, user checks the checkboxs in the listview. when multiple checkboxs are selected, the selected data should be stored in mysql database. i have adapter class to get the data form database. but unable to store the selected data from listview.

any help would be appreciated. thank u.

here is my code for main Activity..

public class CallplanActivity extends Activity implements
    OnItemSelectedListener {

CheckBox chbk;

ListView lv;

Context context;

Spinner city;

ImageButton done;

ProgressDialog pDialog;

private ArrayList<City> cityList;

JSONParser jParser = new JSONParser();

JSONArray doctors_info;

ListView listview;
ListViewAdapter adapter,adaper2;

String cityselected;
ArrayAdapter<String> adapter1;

ArrayList<Doclist> doclist = new ArrayList<Doclist>();

List<String> cities = new ArrayList<String>();

ArrayList<HashMap<String, String>> peopleList;

private String URL_CITY = "http://ift.tt/1enJZ2k      /select_city.php";
private String URL_DOCTORS = "http://ift.tt/1Hq8S4E";

private static final String TAG_DOCDETAILS = "doctors_details";
static final String TAG_DOC_NAME = "doc_name";
static final String TAG_DOC_QUALI = "qualification";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_callplan);
    // lv = getListView();

    city = (Spinner) findViewById(R.id.city_spinner);
    listview = (ListView) findViewById(R.id.listview);

    peopleList = new ArrayList<HashMap<String, String>>();
    done=(ImageButton)findViewById(R.id.done);

    adapter = new ListViewAdapter(CallplanActivity.this, peopleList);

    cityList = new ArrayList<City>();

    city.setOnItemSelectedListener(this);

    listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


    new GetCity().execute();

    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        SparseBooleanArray checked = listview.getCheckedItemPositions();
             ArrayList<String> selectedItems = new ArrayList<String>();


             for (int i = 0; i < checked.size(); i++) {
                    // Item position in adapter
                    int position = checked.keyAt(i);
                    // Add sport if it is checked i.e.) == TRUE!
                    if (checked.valueAt(i))
               selectedItems.add((String) adapter.getItem(position));
                        //adaper2.areAllItemsEnabled();
             String[] outputStrArr = new String[selectedItems.size()];

                    for (int j = 0; j < selectedItems.size(); j++) {
                        outputStrArr[j] = selectedItems.get(j);
                    }
 Toast.makeText(getApplicationContext(), "sagar",  Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(getApplicationContext(),
                          DetailingActivity.class);

                    // Create a bundle object
                    Bundle b = new Bundle();
                    b.putStringArray("selectedItems", outputStrArr);

                    // Add the bundle to the intent.
                   intent.putExtras(b);

                    // start the ResultActivity
                    startActivity(intent);
                }
                }               

    });


}



private void populateSpinner() {

    // txtCategory.setText("");

    for (int i = 0; i < cityList.size(); i++) {
        cities.add(cityList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, cities);

    // Drop down layout style - list view with radio button
    spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    city.setAdapter(spinnerAdapter);
}

private class GetCity extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CallplanActivity.this);
        pDialog.setMessage("Fetching cities..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CITY,
                ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("doctors_details");

                    for (int i = 0; i < categories.length(); i++) {
                    JSONObject catObj = (JSONObject) categories.get(i);
                        City cat = new City(catObj.getString("city"));
                        cityList.add(cat);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }

  }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
 //Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.callplan, menu);
    return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
   }

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    // new GetCategories().execute();
    Spinner city1 = (Spinner) parent;

    if (city1.getId() == R.id.city_spinner) {

        cityselected = cities.get(position);

        Toast.makeText(getApplicationContext(), cityselected,
                Toast.LENGTH_LONG).show();

        new GetDoctorsDetails().execute();

    } else {
        Toast.makeText(getApplicationContext(),
            "Please Select City from Dropdown Box", Toast.LENGTH_LONG)
                .show();
    }
}

class GetDoctorsDetails extends AsyncTask<String, String, JSONObject> {

    JSONObject jsonObject;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // TODO Auto-generated method stub

        String city_name = cityselected.toString();
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("city", city_name));

    jsonObject = jParser.makeHttpRequest(URL_DOCTORS, "POST", params1);

        Log.e("Response: ", "> " + jsonObject.toString());
        return jsonObject;

    }

    @Override
    protected void onPostExecute(JSONObject json) {
        adapter.data.clear();

        pDialog.dismiss();
        if (json != null) {
            try {
                doctors_info = json.getJSONArray(TAG_DOCDETAILS);

                Log.e("Response: ", "> " + doctors_info.toString());

                for (int i = 0; i < doctors_info.length(); i++) {
                    JSONObject c = doctors_info.getJSONObject(i);

                    String doc_name = c.getString(TAG_DOC_NAME);
                    String qualification = c.getString(TAG_DOC_QUALI);

            HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_DOC_NAME, doc_name);
                    map.put(TAG_DOC_QUALI, qualification);

                    Log.d("TAG_DOC_NAME", "" + doc_name);
                    Log.d("TAG_DOC_QUALI", "" + qualification);

                    peopleList.add(map);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // Set the adapter to the ListView
            listview.setAdapter(adapter);

            adapter.notifyDataSetChanged();
        }

    }

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}
}

this is adapter class..

public class ListViewAdapter extends BaseAdapter {

Context context;
LayoutInflater inflater;

ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    // imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final TextView docname;
    TextView qualification;
    CheckBox chbk;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater
            .inflate(R.layout.doc_list_items, parent, false);

    resultp = data.get(position);

    docname = (TextView) itemView.findViewById(R.id.doc_name);
    qualification = (TextView) itemView
            .findViewById(R.id.doc_qualification);
    chbk = (CheckBox) itemView.findViewById(R.id.select_doc);

    docname.setText(resultp.get(CallplanActivity.TAG_DOC_NAME));
    qualification.setText(resultp.get(CallplanActivity.TAG_DOC_QUALI));


    return itemView;
}

}




Aucun commentaire:

Enregistrer un commentaire