簡體   English   中英

動態填充Android Spinner

[英]Populate android spinner dynamically

我已經在android中創建了文本視圖,如下所示:

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

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

我創建了一個微調框,方法是:

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

但是我無法在微調器中體現價值。 大多數教程都提供了使用數組適配器填充旋轉器的方法,但是它使用的是XML的ID,例如R.id。 ....由於我是動態創建的,所以我不能那樣做。 如何動態填充微調框?

您可以將值存儲在String數組中,然后使用該數組填充微調器。

像這樣的String[] value = new String[]{ "one","two","three".....};

也可以嘗試此操作ArrayList<String> list=new ArrayList<String>();

list.add(your_value);

然后String[] value=list.toArray();

您需要創建SpinnerAdapter或BaseAdapter的拼版並將其設置為Spinner的適配器。

http://developer.android.com/reference/android/widget/SpinnerAdapter.html

您可以通過適配器從陣列返回視圖。

另外,請考慮重命名變量。 它們不是很具描述性,並且給變量11命名是不好的做法。

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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM