Populate Android Spinner Dynamically
I have created a Text View in android like the code below: LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll); txt
Solution 1:
You can store the values in a String array then use that array to populate your spinner.
Like this String[] value = new String[]{ "one","two","three".....};
Try this too ArrayList<String> list=new ArrayList<String>();
list.add(your_value);
then String[] value=list.toArray();
Solution 2:
You need to create an impementation of SpinnerAdapter or BaseAdapter and set it as the adapter of the Spinner.
http://developer.android.com/reference/android/widget/SpinnerAdapter.html
You return the views through the adapter from the array.
Also please consider renaming your variables. They aren't very descriptive and naming a variable 11 is poor practice.
Solution 3:
HashMap<String, String> map = newHashMap<>();
map.put("token", prefrences.getUserData().getToken());
map.put("u_id", prefrences.getUserData().getId());
CaregoryID.add("0");
Category.add("Select Category");
appDialogs.showProgressDialog();
callAPiActivity.doPost((Activity) mContext, map, "URL NAME", newGetApiResult() {
@OverridepublicvoidonSuccesResult(JSONObject result) throws JSONException {
appDialogs.hideProgressDialog();
JSONArray countryArray = result.getJSONArray("data");
for (int i = 0; i < countryArray.length(); i++) {
JSONObject countryObj = countryArray.getJSONObject(i);
CaregoryID.add(countryObj.getString("c_id"));
Category.add(countryObj.getString("c_title_price"));
}
categoryAdapter = newArrayAdapter(mContext, R.layout.simple_spinner_item, Category);
categoryAdapter.setDropDownViewResource( android.R.layout.simple_list_item_1);
spinnerCategory.setAdapter(categoryAdapter);
}
}
Post a Comment for "Populate Android Spinner Dynamically"