在开始时间小时分钟和上午,下午有三个微调器。我希望获得在单个参数中绑定的三个微调器的值,并在将其发布到数据库时发送它。我的代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_event);
mContext = AddEventActivity.this;
init();
setOnClick();
}
private void init() {
e2aPreferences = new E2aPreferences(mContext);
sp_am = (Spinner) findViewById(R.id.sp_am);
sp_hh = (Spinner) findViewById(R.id.sp_hh);
sp_mm = (Spinner) findViewById(R.id.sp_mm);
sp_amed = (Spinner) findViewById(R.id.sp_amed);
sp_mmed = (Spinner) findViewById(R.id.sp_mmed);
sp_hhed = (Spinner) findViewById(R.id.sp_hhed);
sp_province = (Spinner) findViewById(R.id.sp_province);
sp_states = (Spinner) findViewById(R.id.sp_states);
tv_province = (TextView) findViewById(R.id.tv_province);
et_title = (EditText) findViewById(R.id.et_title);
et_desc = (EditText) findViewById(R.id.et_desc);
et_venue = (EditText) findViewById(R.id.et_venue);
bt_Okay = (Button) findViewById(R.id.bt_Okay);
setHourSpinnerAdapter();
setMinSpinnerAdapter();
setAmSpinnerAdapter();
callWebToSpinnersAndSetData();
}
private void setOnClick() {
sp_am.setOnItemSelectedListener(this);
sp_hh.setOnItemSelectedListener(this);
sp_mm.setOnItemSelectedListener(this);
sp_amed.setOnItemSelectedListener(this);
sp_mmed.setOnItemSelectedListener(this);
sp_hhed.setOnItemSelectedListener(this);
sp_province.setOnItemSelectedListener(this);
sp_states.setOnItemSelectedListener(this);
bt_Okay.setOnClickListener(this);
}
private void setHourSpinnerAdapter() {
List<String> list_hh = new ArrayList<String>();
list_hh.add("00");
list_hh.add("01");
list_hh.add("02");
list_hh.add("03");
list_hh.add("04");
list_hh.add("05");
list_hh.add("06");
list_hh.add("07");
list_hh.add("08");
list_hh.add("09");
list_hh.add("10");
list_hh.add("11");
list_hh.add("12");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, list_hh);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_hh.setAdapter(dataAdapter);
sp_hhed.setAdapter(dataAdapter);
}
private void setMinSpinnerAdapter() {
List<String> list_mm = new ArrayList<String>();
list_mm.add("00");
list_mm.add("01");
list_mm.add("02");
list_mm.add("03");
list_mm.add("04");
list_mm.add("05");
list_mm.add("06");
list_mm.add("07");
list_mm.add("08");
list_mm.add("09");
list_mm.add("10");
list_mm.add("11");
list_mm.add("12");
list_mm.add("13");
list_mm.add("14");
list_mm.add("15");
list_mm.add("16");
list_mm.add("17");
list_mm.add("18");
list_mm.add("19");
list_mm.add("26");
list_mm.add("27");
list_mm.add("28");
list_mm.add("29");
list_mm.add("30");
list_mm.add("31");
list_mm.add("32");
list_mm.add("33");
list_mm.add("20");
list_mm.add("21");
list_mm.add("22");
list_mm.add("23");
list_mm.add("24");
list_mm.add("25");
list_mm.add("34");
list_mm.add("35");
list_mm.add("36");
list_mm.add("37");
list_mm.add("38");
list_mm.add("39");
list_mm.add("40");
list_mm.add("41");
list_mm.add("42");
list_mm.add("43");
list_mm.add("44");
list_mm.add("45");
list_mm.add("46");
list_mm.add("49");
list_mm.add("50");
list_mm.add("47");
list_mm.add("48");
list_mm.add("52");
list_mm.add("53");
list_mm.add("54");
list_mm.add("55");
list_mm.add("56");
list_mm.add("57");
list_mm.add("58");
list_mm.add("59");
ArrayAdapter<String> dataAdapter_mm = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, list_mm);
dataAdapter_mm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_mm.setAdapter(dataAdapter_mm);
sp_mmed.setAdapter(dataAdapter_mm);
}
private void setAmSpinnerAdapter() {
List<String> list_am = new ArrayList<String>();
list_am.add("AM");
list_am.add("PM");
ArrayAdapter<String> dataAdapter_am = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item, list_am);
dataAdapter_am.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_am.setAdapter(dataAdapter_am);
sp_amed.setAdapter(dataAdapter_am);
}
private void callWebToSpinnersAndSetData() {
new MakeRequestForGetStates().execute();
}
private void validateAndPostData() {
String title = et_title.getEditableText().toString();
String description = et_desc.getEditableText().toString();
String venue = et_venue.getEditableText().toString();
if (title.equalsIgnoreCase("")) {
showMessage("Please enter title");
} else if (description.equalsIgnoreCase("")) {
showMessage("Please enter description");
} else if (venue.equalsIgnoreCase("")) {
showMessage("Please enter venue");
} else {
if (Utills.isConnectionAvailable(mContext)) {
new MakeRequestForAddEvent(title, description, start_date, end_date, "-1", e2aPreferences.getUserIdValue(), venue, "2", "3", "true", "true").execute();
} else {
showMessage("No internet connection, Retry.");
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.sp_states:
String idToSendForCities = statesBeanArrayList.get(position).getStateID();
stateIdSend = idToSendForCities;
if (countclickedState == 1) {
new MakeRequestForGetProvinces(idToSendForCities).execute();
}
countclickedState = 1;
selectedState = sp_states.getSelectedItem().toString();
break;
case R.id.sp_province:
cityIdSend = provinceBeanArrayList.get(position).getCityID();
selectedProvince = sp_province.getSelectedItem().toString();
break;
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_Okay:
AppController.getInstance().isProfileUpdated = true;
validateAndPostData();
break;
}
}
private class MakeRequestForGetStates extends AsyncTask<String, Void, String> {
private String response;
private MakeServiceClass makeServiceClass = new MakeServiceClass();
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> parms = new HashMap<String, String>();
response = makeServiceClass.makeServiceConnectionPost(Const.STATE_DETAILS_URL, parms);
Log.e("response states", response);
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(response);
if (response != null) {
try {
JSONObject mainObject = new JSONObject(response);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONArray datArray = mainObject.getJSONArray("Data");
statesBeanArrayList = new ArrayList<>();
if (datArray.length() > 0) {
for (int i = 0; i < datArray.length(); i++) {
StatesBean statesBean = new StatesBean();
JSONObject internalDataObject = datArray.getJSONObject(i);
if (internalDataObject.has("StateID")) {
statesBean.setStateID(internalDataObject.getString("StateID"));
}
if (internalDataObject.has("Title")) {
statesBean.setTitle(internalDataObject.getString("Title"));
}
statesBeanArrayList.add(statesBean);
}
setStatesSpinnerdata();
} else {
showMessage("No occupations found for your profile.");
}
}
} else {
showMessage("Server error, Please try again");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private class MakeRequestForGetProvinces extends AsyncTask<String, Void, String> {
private String response, stateIdNew;
private MakeServiceClass makeServiceClass = new MakeServiceClass();
MakeRequestForGetProvinces(String id) {
this.stateIdNew = id;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> parms = new HashMap<String, String>();
parms.put("StateID", stateIdNew);
response = makeServiceClass.makeServiceConnectionPost(Const.GETCITIES_FORSTATE_URL, parms);
Log.e("response cities", response);
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(response);
if (response != null) {
try {
JSONObject mainObject = new JSONObject(response);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONArray datArray = mainObject.getJSONArray("Data");
provinceBeanArrayList = new ArrayList<>();
if (datArray.length() > 0) {
for (int i = 0; i < datArray.length(); i++) {
ProvinceBean provinceBean = new ProvinceBean();
JSONObject internalDataObject = datArray.getJSONObject(i);
if (internalDataObject.has("CityID")) {
provinceBean.setCityID(internalDataObject.getString("CityID"));
}
if (internalDataObject.has("Title")) {
provinceBean.setTitle(internalDataObject.getString("Title"));
}
provinceBeanArrayList.add(provinceBean);
}
setCitiesSpinnerdata();
} else {
showMessage("No province found for your profile.");
}
}
} else {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void setStatesSpinnerdata() {
String stateName = "";
for (int i = 0; i < statesBeanArrayList.size(); i++) {
if (statesBeanArrayList.get(i).getStateID().equalsIgnoreCase(stateId)) {
stateName = statesBeanArrayList.get(i).getTitle();
break;
}
}
stateIdSend = stateId;
ArrayList<String> allData = new ArrayList<>();
if (!stateName.equalsIgnoreCase("")) {
allData.add(0, stateName);
selectedState = stateName;
} else {
allData.add(0, "Select Province");
selectedState = "Select Province";
}
for (int i = 1; i < statesBeanArrayList.size(); i++) {
allData.add(statesBeanArrayList.get(i).getTitle());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, allData);
// Drop down followup_details_layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_states.setAdapter(dataAdapter);
}
private void setCitiesSpinnerdata() {
String cityName = "";
for (int i = 0; i < provinceBeanArrayList.size(); i++) {
if (provinceBeanArrayList.get(i).getCityID().equalsIgnoreCase(cityId)) {
cityName = provinceBeanArrayList.get(i).getTitle();
break;
}
}
cityIdSend = cityId;
ArrayList<String> allData = new ArrayList<>();
if (!cityName.equalsIgnoreCase("")) {
allData.add(0, cityName);
selectedProvince = cityName;
} else {
allData.add(0, "Select City");
selectedProvince = "Select City";
}
for (int i = 1; i < provinceBeanArrayList.size(); i++) {
allData.add(provinceBeanArrayList.get(i).getTitle());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, allData);
// Drop down followup_details_layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_province.setAdapter(dataAdapter);
}
private void showMessage(String message) {
Snackbar snackbar = Snackbar
.make(main, message, Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
snackbar.show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
private class MakeRequestForAddEvent extends AsyncTask<String, Void, String> {
private String response;
private MakeServiceClass makeServiceClass = new MakeServiceClass();
String Title, Description, starttime, endtime, EventId, UserID, Venue, StateID, CityID, IsAdmin, IsShow;
MakeRequestForAddEvent(String Title, String Description, String starttime, String endtime, String EventId, String UserID, String Venue, String StateID, String CityID, String IsAdmin, String IsShow) {
this.Title = Title;
this.Description = Description;
this.starttime = starttime;
this.endtime = endtime;
this.EventId = EventId;
this.UserID = UserID;
this.Venue = Venue;
this.StateID = StateID;
this.CityID = CityID;
this.IsAdmin = IsAdmin;
this.IsShow = IsShow;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> parms = new HashMap<String, String>();
parms.put("Title", Title);
parms.put("Description", Description);
parms.put("StartDate", starttime);
parms.put("EndDate", endtime);
parms.put("EventID", EventId);
parms.put("UserID", e2aPreferences.getUserIdValue());
parms.put("Venue", Venue);
parms.put("StateID", StateID);
parms.put("CityID", CityID);
parms.put("IsAdmin", IsAdmin);
parms.put("IsShow", IsShow);
response = makeServiceClass.makeServiceConnectionPost(Const.ADD_EVENT_URL, parms);
Log.e("response addevent", response);
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(response);
if (response != null) {
// pb_dashboard.setVisibility(View.GONE);
showMessage("Event has been saved successfully!");
try {
JSONObject mainObject = new JSONObject(response);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONObject dataObj = mainObject.getJSONObject("Data");
String Message = dataObj.getString("Message");
if (Message.equalsIgnoreCase("Success")) {
((AddEventActivity) mContext).showMessage("Client has been added successfully.");
finish();
}
}
}
} else {
showMessage("Server error, Retry.");
((AddEventActivity) mContext).showMessage("error while adding event");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}