Reputation: 1535
I am trying a simple app where I populating a spinner with the values (json value) I got from network call. But I am not getting how to get the associated json value, when an item selected from the spinner. Can any one provide me a article/reference on how to do this?
this is my json response
{
item: {
name: "xyz",
brands: [
{
id: 123,
name: "abc"
}
]
}
}
This is how I parsed the json.
public class user_activity extends ActionBarActivity {
RequestQueue queue;
ArrayList<String> brand_list = new ArrayList<String>();
Spinner spinner;
String brand_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_activity);
this.spinner= (Spinner) findViewById(R.id.spinner);
loadUser();
}
public void loadUser() {
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please Wait..");
pDialog.show();
queue = Volley.newRequestQueue(this);
String url = "http://myApiUrl";
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
pDialog.hide();
try {
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
String b = array.getJSONObject(i).getString("name");
brand_list.add(b);
}
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(user_activity.this,android.R.layout.simple_dropdown_item_1line,brand_list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(),spinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
} catch (JSONException e) {
e.printStackTrace();
pDialog.hide();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", String.valueOf(error));
}
}
);
queue.add(postRequest);
}
I want to send the associated id values i,e "123" to next activity through a button intent.
EDIT: Modified code as follows
But function set spinner always returns empty
public class user_activity extends ActionBarActivity {
String phone_number;
String access_token;
Button enterButton;
String brand_id;
TextView uName;
RequestQueue queue;
ArrayList<String> brand_list = new ArrayList<String>();
ArrayList<String> _ids = new ArrayList<String>();
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_activity);
this.phone_number = getIntent().getStringExtra("phone");
this.access_token = getIntent().getStringExtra("token");
enterButton = (Button)findViewById(R.id.buttonEnter);
uName = (TextView) findViewById(R.id.tvName);
this.spinner= (Spinner) findViewById(R.id.spinner);
loadUser(phone_number, access_token);
// nextActivity(b[0]);
}
public void loadUser(String phone, String token) {
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please Wait..");
pDialog.show();
queue = Volley.newRequestQueue(this);
final String[] message = new String[1];
final String[] my_id = new String[1];
String url = "http://api_url";
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
pDialog.hide();
try {
message[0] = response.getJSONObject("item").getString("name");
uName.setText(message[0]);
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
String b = array.getJSONObject(i).getString("name");
String id = array.getJSONObject(i).getString("id");
_ids.add(id);
brand_list.add(b);
}
String id = setSpinner();
} catch (JSONException e) {
e.printStackTrace();
pDialog.hide();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", String.valueOf(error));
}
}
);
queue.add(postRequest);
}
public String setSpinner(){
ArrayAdapter<String> adapter;
final String[] id = new String[1];
adapter = new ArrayAdapter<String>(user_activity.this,android.R.layout.simple_dropdown_item_1line,brand_list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id[0] = _ids.get(i);
// Toast.makeText(getBaseContext(), id[0] + ":" + spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Toast.makeText(getBaseContext(), id[0].toString() , Toast.LENGTH_SHORT).show();
return id[0];
}
Upvotes: 1
Views: 5273
Reputation: 5973
I think you need to store the data first in an array list like below..
JSONArray array = response.getJSONObject("item").getJSONArray("brands");
for (int i=0; i< array.length();i++) {
JSONObject mItemObject = array.getJSONObject(i);
String b = mItemObject.getString("name");
brand_list.add(b);
JSONArray mBrandsArray = mItemObject.getJSONArray("brands");
JSONObject mBrandsObject = mBrandsArray.getJSONObject(0);
String brandsId = mBrandsObject.getString("id");
String brandsName = mBrandsObject.getString("name");
brand_id.add(brandId); //make this array list likebrand_list
brand_name.add(brandName); //make this array list like ..
}
And then in your spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(),spinner.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
Intent mIntent = new Intent(this, NewActivity.class);
// load data using putExtra
mIntent.putExtra("id", brand_id.get(i));
...
...
startActivity(mIntent);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Upvotes: 3
Reputation: 47817
There are many ways.
One way is create id
arrayList
ArrayList<String> _ids = new ArrayList<String>();
add values from JSON
to this arryList
String id = array.getJSONObject(i).getString("id");
_ids.add(id);
and then used it in your spinner.setOnItemSelectedListener(....)
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String id=_ids.get(position);//Used this id to pass to another Activity
}
....
});
Upvotes: 5
Reputation: 2446
You need to create your own omplementation of OnItemSelectedListener
interface and place it to spinner by spinner.setOnItemSelectedListener(listener)
. Your listener
object should look like
listener = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
JSONObject selectedObject = globalItemsObject.optJSONObject(position);
//make some actions with "selectedObject"
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
Upvotes: 0
Reputation: 1215
Create a Class called Brand as below
class Brand {
int id;
String name;
@Override
public String toString() {
return name;
}
}
Now change your parsing code and make the Brand object in place of just getting name and add it to brand_list
. Once user select anything you can get the selected index find the brand object and send the id from object to the other activity.
Upvotes: 0