Kunal Agrawal Kunal Agrawal - 1 month ago 8
Android Question

i am getting a error as wrong argument found for implementing material spinner

i am getting a error Wrong 2nd argument type. Found: 'com.jaredrummler.materialspinner.MaterialSpinner', required: 'java.lang.String' at level string

protected Map<String, String> getParams() {

// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("phone",phone);
params.put("level", level);

return params;
}


i am not able to find out where is wrong script

public class ActivityProfile extends AppCompatActivity {
private static final String TAG = ActivityProfile.class.getSimpleName();
private SQLiteHandler db;
private SessionManager session;
private EditText editname,editemail,editphone;
private Toolbar toolbar;
private Button next;
private MaterialSpinner level;

private static final String[] LEVEL = {
"UnderGraduate" ,"Graduate"
};

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

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar = (Toolbar) findViewById(R.id.toolbar);


editname =(EditText) findViewById(R.id.name);
editemail =(EditText) findViewById(R.id.email);
editphone = (EditText) findViewById(R.id.phone);
level =(MaterialSpinner) findViewById(R.id.level);
next = (Button) findViewById(R.id.next);
db = new SQLiteHandler(getApplicationContext());
session = new SessionManager(getApplicationContext());

// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();

String name = user.get("name");
String email =user.get("email");
String phone = user.get("phone");
// Displaying the user details on the screen
editname.setText(name);
editemail.setText(email);
editphone.setText(phone);


final MaterialSpinner level = (MaterialSpinner) findViewById(R.id.level);
level.setItems(LEVEL);
level.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
}
});
level.setOnNothingSelectedListener(new MaterialSpinner.OnNothingSelectedListener() {

@Override public void onNothingSelected(MaterialSpinner spinner) {
Snackbar.make(spinner, "Nothing selected", Snackbar.LENGTH_LONG).show();
}
});


next.setOnClickListener(new View.OnClickListener(){

public void onClick(View view) {
String name = editname.getText().toString();
String email = editemail.getText().toString();
String phone = editphone.getText().toString();

createprofile(name, email,phone,level);



}


});

}

private void createprofile(final String name, final String email,
final String phone,final MaterialSpinner level) {

StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_CREATEPROF1, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, "Profile Response: " + response.toString());


try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {



Toast.makeText(getApplicationContext(), "User successfully registered.", Toast.LENGTH_LONG).show();

// Launch login activity
Intent intent = new Intent(
ActivityProfile.this,
VerifyOtp.class);
startActivity(intent);
finish();
} else {

// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();

}
}) {

@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("phone",phone);
params.put("level", level);

return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq);
}


}


pls help me out to correct error

Answer

This is because you try to put another value in a map, different than a String.

You have a map:

Map<String, String> params = new HashMap<String, String>();

that has String values (see the second String word in <String, String>), but level is an instance of MaterialSpinner, so the following line fails:

params.put("level", level);

Define a toString method in MaterialSpinner and use:

params.put("level", level.toString());

instead.

Alternatively, you can try to get a String value from your level (E.g.: level.getName() - instead of level.toString()), or (if you really need to store that object) you need to change the map to:

Map<String, Object> params = new HashMap<String, Object>();

If you use JDK 7 or 8, you can also simplify it to:

Map<String, Object> params = new HashMap<>();