0

I have created a Text View in android like the code below:

 LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);

txtv = new TextView(this);
txtv.setText("text");
ll.addView(txtv);     

Same way I have created a spinner:

spinner = new Spinner(this);
ll.addView(spinner);

But I am unable to populate value on the spinner. Most tutorials giving populating spinner with array adapter but it is taking id of xml, like R.id. .... Since I am creating dynamic I cant do like that way. How can I populate spinner dynamically?

Paulo Rodrigues
  • 5,273
  • 7
  • 35
  • 58
androidGenX
  • 1,108
  • 3
  • 18
  • 44
  • You can create one instance of `ArrayAdapter` add the text you enter in `TextView` into instance of `ArrayAdapter` and set that instance to spinner. You can make it on just one button click – Akshay Mar 18 '14 at 12:58
  • duplicate [here](http://stackoverflow.com/questions/22479597/populate-android-spinner-dynamically) – NameSpace Mar 18 '14 at 13:07

3 Answers3

0

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();

suresh kumar
  • 193
  • 1
  • 10
0

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.

DoctorDbx
  • 328
  • 4
  • 12
0
HashMap<String, String> map = new HashMap<>();

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", new GetApiResult() {
    @Override
    public void onSuccesResult(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 = new ArrayAdapter(mContext, R.layout.simple_spinner_item, Category);

        categoryAdapter.setDropDownViewResource( android.R.layout.simple_list_item_1);
        spinnerCategory.setAdapter(categoryAdapter);

    }
}
quinz
  • 1,282
  • 4
  • 21
  • 33
jay
  • 1