0

In my android app, For country or city I have used spinner widget from android . In that scenario, firstly, load data from database into spinner it is work perfectly. I have country list in spinner which is load from database. My problem is that, when user Click on spinner for country selection at that time user will get error. I attached error log please refer it. Help me. Thanks in advance. My problem is solved but I got a new error. See My error log enter image description here Full source code

 Private class Question2Activity extends AppCompactActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
 {
 private static final String PATH_TO_SERVER = "http://192.168.1.103/lab/android_login_example/selectcountries.php";
private static final String SELECT_CITI = "http://192.168.1.103/lab/android_login_example/selectciti.php";

  AppCompatSpinner spCity, upcountry;

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question2);

    mContext = getApplicationContext();
    // Get the activity
    mActivity = Question2Activity.this;

    // activity_question_detail = (RelativeLayout) findViewById(R.id.activity_question_detail);

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

    // Inflate the custom layout/view
    View customView = inflater.inflate(R.layout.raw_question2, null);

    mPopupWindow = new android.widget.PopupWindow(
            customView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    );

    mPopupWindow.setAnimationStyle(R.style.PopupAnimation);

    if (Build.VERSION.SDK_INT >= 21) {
        mPopupWindow.setElevation(5.0f);
    }

    mPopupWindow.setFocusable(true);
    mPopupWindow.update();

    spCountry = (AppCompatSpinner) customView.findViewById(R.id.spCountry);
    spCity = (AppCompatSpinner) customView.findViewById(R.id.spCity);
      btnNext = (Button) customView.findViewById(R.id.btnNext);

    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);


    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Question2User(stCountry, stCity);
            finish();
        }

    });


    requestJsonObject();
    requestJsonObjectforCiti();

    spCountry.setOnItemSelectedListener(this);
    spCity.setOnItemSelectedListener(this);

    findViewById(R.id.activity_question_detail).post(new Runnable() {
        public void run() {
            mPopupWindow.showAtLocation(findViewById(R.id.activity_question_detail), Gravity.CENTER, 0, 0);
        }
    });
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    if (adapterView.getId() == R.id.spCountry) {

        stCountry = spCountry.getSelectedItem().toString();
        Log.d("spinner", spCountry.getSelectedItem().toString());
    } else if (adapterView.getId() == R.id.spCity) {

        stCity = spCity.getSelectedItem().toString();
        Log.d("spinner", spCity.getSelectedItem().toString());
    }
}

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

}
}

 private void requestJsonObject() {
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, PATH_TO_SERVER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
            //display first question to the user
            if (null != spinnerData) {
                assert spCountry != null;
                adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
                spCountry.setAdapter(spinnerAdapter);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    queue.add(stringRequest);
}

private void requestJsonObjectforCiti() {
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, SELECT_CITI, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            spinnerData = Arrays.asList(mGson.fromJson(response, DataObject[].class));
            //display first question to the user
            if (null != spinnerData) {
                assert spCity != null;
                adapter.SpinnerAdapter spinnerAdapter = new adapter.SpinnerAdapter(Question2Activity.this, spinnerData);
                spCity.setAdapter(spinnerAdapter);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    queue.add(stringRequest);
}
Developer_99
  • 135
  • 1
  • 7
  • 17

1 Answers1

1

you have to implement your class with AdapterView.OnItemSelectedListener and override method this method the code is given below may be helpful fo you int the XML file

 @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        if(adapterView.getId()==R.id.ueducation) {
            uqlification = qulification.getSelectedItem().toString();
            Log.d("spinner", qulification.getSelectedItem().toString());
        }
        else
        if(adapterView.getId()==R.id.uocuuaption) {
         uoccupation=occupation.getSelectedItem().toString();
            Log.d("spinner", occupation.getSelectedItem().toString());
        }

    }

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

    }



    <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/ueducation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_5sdp"
            android:entries="@array/education"
            android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/uocuuaption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_5sdp"
            android:entries="@array/ustatus"
            android:padding="@dimen/_10sdp"></android.support.v7.widget.AppCompatSpinner>

Look at the Example:

public class User_Detail extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {

    AppCompatSpinner qulification, occupation;
    String uqlification="Select",uoccupation="Select";



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_basic_details);

        //fetch the spinners view
        qulification = (AppCompatSpinner) findViewById(R.id.ueducation);
        occupation = (AppCompatSpinner) findViewById(R.id.uocuuaption);


        //attac the setItemSelected linstener
        qulification.setOnItemSelectedListener(this);
        occupation.setOnItemSelectedListener(this);

    }



    }




    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        if(adapterView.getId()==R.id.ueducation) {
            uqlification = qulification.getSelectedItem().toString();
            Log.d("spinner", qulification.getSelectedItem().toString());
        }
        else
        if(adapterView.getId()==R.id.uocuuaption) {
         uoccupation=occupation.getSelectedItem().toString();
            Log.d("spinner", occupation.getSelectedItem().toString());
        }

    }

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

    }
}
  • remove the spinner to onclickView listener – Mahendra Dabi Feb 13 '17 at 05:51
  • Please see my updated post I attached screenshot , I have a problem regarding Dropdown I tried your solution. When I click on dropdown It is not clickable please help me. Thank you in advance @Mahendra Dabi – Developer_99 Feb 13 '17 at 08:05
  • apply the listener on both the spinner as given below so your on changeItemListnere will call qulification.setOnItemSelectedListener(this); occupation.setOnItemSelectedListener(this); – Mahendra Dabi Feb 13 '17 at 08:59
  • check my Edited post I have given an example which may be helpful for you. if you found this helpful please upvote this answer thank You ...Happy coding – Mahendra Dabi Feb 13 '17 at 09:04
  • I apply this solution I got clickable interface , Please see my updated post I got a popupwindow error please help me. @Mahendra Dabi – Developer_99 Feb 13 '17 at 10:02
  • Richa can you please post the entire code so I can better understand your problem – Mahendra Dabi Feb 13 '17 at 10:36
  • Post full source code please check it. @Mahendra Dabi – Developer_99 Feb 13 '17 at 10:49
  • have you Override onItemSelected and Override onNothingSelected I think you and not implements your class View.OnClickListener,AdapterView.OnItemSelectedListener if you have done it. then where is your override method. post the code of entire class not just onCreate method – Mahendra Dabi Feb 13 '17 at 10:54
  • Sorry but I have pressure to solve this query any way , Post full source code please check it. @Mahendra Dabi if you can solve this problem then help me and thank you so much for your efferts. – Developer_99 Feb 13 '17 at 11:04
  • this link will be helpful for you try it: http://stackoverflow.com/questions/8803915/window-manager-bad-token-exception – Mahendra Dabi Feb 13 '17 at 11:36
  • I referred your suggestion look but I faced still that error. @Mahendra Dabi – Developer_99 Feb 14 '17 at 05:00
  • I also faced the same problem you can your ActivityName.this in popup context instead of passing context variable it'll solve my problem may be helpful for you. If you found solution helpful please upvote it and accept as answer. Thank You – Mahendra Dabi Feb 15 '17 at 14:02
  • Yes, actually my problem is that In marshmallow does not support spinner mode dropdown so I will change dropdown to dialog mode it's work. Thank you so much for your efferts. @Mahendra Dabi – Developer_99 Feb 16 '17 at 07:45