动态填充android微调器

问题描述 投票:0回答:3

我在android中创建了一个Text View,如下面的代码所示:

 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. ....因为我正在创造动态我不能这样做。如何动态填充微调器?

android
3个回答
0
投票

您可以将值存储在String数组中,然后使用该数组填充微调器。

喜欢这个String[] value = new String[]{ "one","two","three".....};

试试这个ArrayList<String> list=new ArrayList<String>();

list.add(your_value);

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


0
投票

您需要创建SpinnerAdapter或BaseAdapter的实现,并将其设置为Spinner的适配器。

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

您可以通过数组中的适配器返回视图。

另外请考虑重命名变量。它们不是很具描述性,命名变量11是不好的做法。


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

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.