Select option is showing in spinner's popped List
I have a problem that I have two different spinners, data comes from json parsing in both of Spinners, When a item is selected from first spinner then second spinner gets filled by data but when nothing is selected in first spinner, it displays only Select option. But when user want to select other item from first spinner List also shows Select option which is not required. Please suggest me how to do that?
Thanks in advance.
Code:
package com.equinix.android.sitevisit;
import java.util.ArrayList;
import java.util.Calendar;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;
import com.equinix.android.adapter.SiteVisit_CagesAdapter;
import com.equinix.android.adapter.SiteVisit_IBXAdapter;
import com.equinix.android.common.LoginScreen;
import com.equinix.android.common.MenuScreen;
import com.equinix.android.common.R;
import com.equinix.android.parsing.Parse_Json;
import com.equinix.android.servicecall.Service_Call;
public class Site_Visit_Details extends Activity {
/** Called when the activity is first created. */
ImageButton _siteVisit_Home, _siteVisit_Logout, _siteVisit_AddVisitor, _siteVisit_StartDateButton, _siteVisit_EndDateButton, _siteVisit_Next, _siteVisit_CheckBox;
EditText _siteVisit_StartDate, _siteVisit_EndDate, _siteVisit_AdditionalInfo;
Spinner _siteVisit_IBX, _siteVisit_Cage;
ArrayAdapter<String> statusAdapter;
ArrayAdapter<String> statusAdapter1;
Bundle bundle;
private int mYear;
private int mMonth;
private int mDay;
private int flagValue, checkBoxFlag=0;
static final int DATE_DIALOG_ID = 0;
public static int count=0;
public static ArrayList<String> cage_Id = new ArrayList<String>();
public static ArrayList<String> cage_Name = new ArrayList<String>();
public static ArrayList<String> cage_NaturalKey = new ArrayList<String>();
public static boolean flag_cages = false;
public static ArrayList<String> firstName = new ArrayList<String>();
public static ArrayList<String> lastName = new ArrayList<String>();
public static ArrayList<String> companyName = new ArrayList<String>();
SharedPreferences prefs;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.site_visit_details);
bundle = getIntent().getExtras();
_siteVisit_Home = (ImageButton)findViewById(R.id.home);
_siteVisit_Logout = (ImageButton)findViewById(R.id.logout);
_siteVisit_AddVisitor = (ImageButton)findViewById(R.id.addVisitorImageButton);
_siteVisit_StartDateButton = (ImageButton)findViewById(R.id.startDateImageButton);
_siteVisit_EndDateButton = (ImageButton)findViewById(R.id.endDateImageButton);
_siteVisit_Next = (ImageButton)findViewById(R.id.nextImageButton);
_siteVisit_CheckBox =(ImageButton)findViewById(R.id.checkBox);
_siteVisit_StartDate = (EditText)findViewById(R.id.startDateEditText);
_siteVisit_EndDate = (EditText)findViewById(R.id.endDateEditText);
_siteVisit_AdditionalInfo = (EditText)findViewById(R.id.additionalInformationEditText);
_siteVisit_IBX = (Spinner)findViewById(R.id.ibxSpinner);
_siteVisit_Cage = (Spinner)findViewById(R.id.cageSpinner);
// statusAdapter = ArrayAdapter.createFromResource( this, R.array.options , android.R.layout.simple_spinner_item);//Sets an status adapter
statusAdapter = new ArrayAdapter<String>( this,android.R.layout.simple_spinner_item, LoginScreen.name);//Sets an status adapter
statusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//Displays drop down spinner
//statusAdapter.add(LoginScreen.name);
SiteVisit_IBXAdapter adapter = new SiteVisit_IBXAdapter(LoginScreen.name, this);
_siteVisit_IBX.setAdapter(statusAdapter);
_siteVisit_Cage.setAdapter(statusAdapter);
if(bundle.getInt("screen_flag")!=0)
{
try{
prefs = getSharedPreferences(PREFS_NAME, 0);
String savedIBXString = prefs.getString("savedIBXString",null);
String savedCagesString = prefs.getString("savedCagesString", null);
String startDate = prefs.getString("startDate", null);
String endDate= prefs.getString("endDate", null);
String addInfo = prefs.getString("additionalInfo", null);
_siteVisit_StartDate.setText(startDate);
_siteVisit_EndDate.setText(endDate);
_siteVisit_AdditionalInfo.setText(addInfo);
for(int i=0;i<LoginScreen.name.size();i++)
{
if(savedIBXString.equals(_siteVisit_IBX.getItemAtPosition(i).toString()))
{
_siteVisit_IBX.setSelection(i);
}
}
for(int i=0;i<cage_Name.size();i++)
{
if(savedCagesString.equals(_siteVisit_Cage.getItemAtPosition(i).toString()))
{
_siteVisit_Cage.setSelection(i);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
_siteVisit_Home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, MenuScreen.class);
startActivity(intent);
}
});
try {
firstName = bundle.getStringArrayList("FirstName");
lastName = bundle.getStringArrayList("LastName");
companyName = bundle.getStringArrayList("Company");
for(int i=0; i<firstName.size(); i++){
System.out.println("Name is ======== "+firstName.get(i)+" "+lastName.get(i)+" Company name=====>> "+companyName.get(i));
}
} catch (Exception e1) {
e1.printStackTrace();
}
_siteVisit_Logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, LoginScreen.class);
startActivity(intent);
}
});
_siteVisit_Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if((_siteVisit_StartDate.getText().toString().length()!=0) &&(_siteVisit_EndDate.getText().toString().length()!=0))
{
Intent intent = new Intent(Site_Visit_Details.this, Site_Visit_Login.class);
intent.putExtra("IBXName", _siteVisit_IBX.getSelectedItem().toString());
intent.putExtra("CagesName", _siteVisit_Cage.getSelectedItem().toString());
intent.putExtra("StartDate", _siteVisit_StartDate.getText().toString());
intent.putExtra("EndDate", _siteVisit_EndDate.getText().toString());
intent.putStringArrayListExtra("FirstName", firstName);
intent.putStringArrayListExtra("LastName", lastName);
intent.putStringArrayListExtra("Company", companyName);
startActivity(intent);
}else{
Toast.makeText(Site_Visit_Details.this, "Please fill all mandatory field", Toast.LENGTH_SHORT).show();
}
}
}
);
// LoginScreen.name.remove(0);
_siteVisit_IBX.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item was selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
try{
//arrayList_clear();
_siteVisit_IBX.invalidate();
//statusAdapter.notifyDataSetChanged();
/*statusAdapter.clear();
statusAdapter1 = new ArrayAdapter<String>( Site_Visit_Details.this,android.R.layout.simple_spinner_item, LoginScreen.name);
statusAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_siteVisit_IBX.setAdapter(statusAdapter1);*/
prefs = PreferenceManager.getDefaultSharedPreferences( getBaseContext());
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",_siteVisit_IBX.getSelectedItem().toString());
if(_siteVisit_IBX.getSelectedItem().equals("Select"))
{
String array[] = new String[]{"Select"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Site_Visit_Details.this, android.R.layout.simple_spinner_item, array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_siteVisit_Cage.setAdapter(adapter);
}
else{
HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/cages");
StringEntity se = new StringEntity("{\"ibx\":{\"credentials\":{\"username\":\"mobileuser1\",\"password\":\"welcome1\"},\"userkey\":\"68782\",\"permissionCode\":\"PLACE_BASIC_SERVICES\",\"id\":\""+LoginScreen.id.get(pos)+"\",\"name\":\""+LoginScreen.name.get(pos)+"\",\"naturalKey\":\""+LoginScreen.naturalKey.get(pos)+"\"}}");
final Service_Call obj = new Service_Call(post, se, "mobileuser1", "welcome1",false);
/*String json_Response = obj.call_Service();
new Parse_Json(json_Response).parse_Cages();*/
/*SiteVisit_CagesAdapter adapter = new SiteVisit_CagesAdapter(Site_Visit_Details.cage_Name, Site_Visit_Details.this);
_siteVisit_Cage.setAdapter(adapter);*/
final ProgressDialog dialog = ProgressDialog.show(Site_Visit_Details.this, "EQUINIX ", "Loading.... ", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
// SiteVisit_CagesAdapter adapter = new SiteVisit_CagesAdapter(Site_Visit_Details.cage_Name, Site_Visit_Details.this);
statusAdapter1 = new ArrayAdapter<String>( Site_Visit_Details.this,android.R.layout.simple_spinner_item, Site_Visit_Details.cage_Name);
statusAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_siteVisit_Cage.setAdapter(statusAdapter1);
dialog.dismiss();
}
};
final Thread checkUpdate = new Thread() {
public void run() {
String json_Response = obj.call_Service();
new Parse_Json(json_Response).parse_Cages();
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
/* private void arrayList_clear() {
Site_Visit_Details.cage_Id.clear();
Site_Visit_Details.cage_Name.clear();
Site_Visit_Details.cage_NaturalKey.clear();
}*/
_siteVisit_Cage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item was selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
prefs = PreferenceManager.getDefaultSharedPreferences( getBaseContext(开发者_运维百科));
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedCagesString",_siteVisit_Cage.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
_siteVisit_StartDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
flagValue = 0;
showDialog(DATE_DIALOG_ID);
}
});
_siteVisit_EndDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
flagValue = 1;
showDialog(DATE_DIALOG_ID);
}
});
_siteVisit_AddVisitor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Site_Visit_Details.this, Site_Visit_AddVisitor.class);
startActivity(intent);
}
});
_siteVisit_CheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(checkBoxFlag==0){
_siteVisit_CheckBox.setBackgroundResource(R.drawable.check_box_checked);
checkBoxFlag=1;
}else{
_siteVisit_CheckBox.setBackgroundResource(R.drawable.check_box_unchecked);
checkBoxFlag=0;
}
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay(flagValue);
}
};
private void updateDisplay(int flag) {
if(flag==0){
_siteVisit_StartDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" "));
}
else{
_siteVisit_EndDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/")
.append(mDay).append("/")
.append(mYear).append(" "));
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("savedIBXString", _siteVisit_IBX.getSelectedItem().toString());
editor.putString("savedCagesString", _siteVisit_Cage.getSelectedItem().toString());
editor.putString("startDate", _siteVisit_StartDate.getText().toString());
editor.putString("endDate", _siteVisit_EndDate.getText().toString());
editor.putString("additionalInfo", _siteVisit_AdditionalInfo.getText().toString());
// Commit the edits!
editor.commit();
}
}
精彩评论