Question

I have a program in android where spinner gets the data dynamically from rest services. what i want to achieve is when the first spinner loads its value, the selected value loads the value of 2nd and third spinner. i want to disable the click of 2nd and third spinner till the spinner gets populated are loaded in all the spinner. I call the function of populating 2nd & 3rd spinner in the end of the program.

public void addItemsOnSpinner1() 
{
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null) {
String value = extras.getString("new_variable_name");
strEmployeeID = value;
}
JSONObject login = new JSONObject();
try
{
login.put("EmployeeID",strEmployeeID);
//login.put("Password", etCountry.getText().toString());
JSONObject finaldata = new JSONObject();
finaldata.put("ProjectRequest", login);
final ConnectToServer connect = new ConnectToServer();
connect.extConnectToServer(HourlyEntry.this,new ConnectToServer.Callback() 
{
public void callFinished(String result)
{
// Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
JSONObject resp = null;
try 
{
resp = new JSONObject(result);
// Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
JSONObject Login1Result = resp.getJSONObject("ProjectResult");
JSONArray DepartmentDetails = Login1Result.getJSONArray("ProjectDetails");
// String strMessage = Login1Result.getString("message");
// Toast.makeText(getBaseContext(), Login1Result.getString("ProjectDetails"), Toast.LENGTH_LONG).show();
//  List<String> list = new ArrayList<String>();
if (!Login1Result.getString("ProjectDetails").equalsIgnoreCase("null")) 
{
//JSONArray DepartmentDetails = Login1Result.getJSONArray("ProjectDetails");

m_Project_list = new ArrayList<String>();
m_projectID_list = new ArrayList<String>();
for (int i = 0; i < DepartmentDetails.length(); i++) 
{
JSONObject m_DepartmentDetails = DepartmentDetails.getJSONObject(i);
// Toast.makeText(getBaseContext(),m_DepartmentDetails.toString() , Toast.LENGTH_LONG).show();
if (!m_DepartmentDetails.getString("ProjectName").equalsIgnoreCase("null")&& !m_DepartmentDetails.getString("ProjectName").equalsIgnoreCase("")) 
{
// list.add(m_DepartmentDetails.getString("ProjectName"));
m_Project_list.add(m_DepartmentDetails.getString("ProjectName"));
// Toast.makeText(getBaseContext(), m_DepartmentDetails.getString("ProjectName"), Toast.LENGTH_LONG).show();
}
if (!m_DepartmentDetails.getString("ProjectID").equalsIgnoreCase("null")&& !m_DepartmentDetails.getString("ProjectID").equalsIgnoreCase("")) 
{
m_projectID_list.add(m_DepartmentDetails.getString("ProjectID"));
//Toast.makeText(getBaseContext(), m_DepartmentDetails.getString("ProjectID"), Toast.LENGTH_LONG).show();
String strProjectID="";
String item2 =  m_DepartmentDetails.getString("ProjectID");
strProjectID = item2;
}
}
}
s1 = (Spinner) findViewById(R.id.spinnerL);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.spin,m_Project_list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
if (m_projectID_list.contains(m_ProjectID)) 
{
s1.setSelection(m_projectID_list.indexOf(m_ProjectID));
}
}
catch (final JSONException e)
{
}
}
}, "http://aapna.azurewebsites.net/Service1/Project", finaldata,
"POST");
connect.execute(finaldata);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
final String item1 = adapter.getItemAtPosition(position).toString();
final String SelectedProjectID = m_projectID_list.get(s1.getSelectedItemPosition());
//  Toast.makeText(getApplicationContext(),
//  SelectedProjectID, Toast.LENGTH_LONG).show();
//  Showing selected spinner item
//  Toast.makeText(getApplicationContext(),
//  "Selected  : " + item1, Toast.LENGTH_LONG).show();
s2.setClickable(false); 
s3.setClickable(false);
addItemsOnSpinner2(SelectedProjectID);
s2.setClickable(true);
addItemsOnSpinner3(SelectedProjectID);
s3.setClickable(true);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
}
Was it helpful?

Solution 2

The only solution I got is to use delay function to solve this kind of spinner as told to me by hariharan.

ADD these two statements in spinner 1 function

    s1.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> adapter, View v,
                            int position, long id)
                    {
                        // On selecting a spinner item

                        final String item1 = adapter.getItemAtPosition(position).toString();

                        final String SelectedProjectID = m_projectID_list.get(s1.getSelectedItemPosition());


                         s2.setEnabled(false);
                         s3.setEnabled(false);


                               addItemsOnSpinner2(SelectedProjectID);

                               addItemsOnSpinner3(SelectedProjectID);
                               delay1();

//                      



                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0)
                    {

                        // TODO Auto-generated method stub

                    }
            });

then use the below function

 public void delay1()
    {
       final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run()
            {

                  s2.setEnabled(true);
                 s3.setEnabled(true);
              }

        }, 3000);
    }

OTHER TIPS

Try this..

Change your first element from m_Project_list ArrayList as Select

And after user selecting spinner you can do other operation below codes inside onCreate

Code

s1.setSelection(0);
s1.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub
           if(pos == 0){
                s2.setEnabled(false);
                s3.setEnabled(false);
           }
           else{
               // do something
               addItemsOnSpinner2(SelectedProjectID);
               s2.setEnabled(true);
               addItemsOnSpinner3(SelectedProjectID);
               s3.setEnabled(true);
           }
              }
        }

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

        }

    });

EDIT

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            Log.i("==========","  handler == > "); 

            Toast.makeText(getBaseContext(), "handler", Toast.LENGTH_LONG).show();

        }

    }, 5000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top