如何从sqlite中将String值设置为spinner?

时间:2016-03-02 08:46:43

标签: android sqlite spinner

这是我的要求我需要从sqlite设置字符串并将其分配给spinner真的很困惑如何做到这一点当我点击编辑按钮这里它将转到下一个活动在这里我将检索所有值我已设置值为edittext但无法设置值到微调器我到目前为止如何做到这一点我所尝试的是:

    RequestQueue queue_company_name = Volley.newRequestQueue(getBaseContext());
    final ArrayList<Model_Account> arrayobj_company_name = new ArrayList<Model_Account>();
    String Url_company_name = "http://xxx/xxx/xxx/AcoountCreatePageLoad.svc/Account/Accouxxxt";
    JsonObjectRequest jsonObjRequest_company_name = new JsonObjectRequest(Request.Method.GET, Url_company_name, new JSONObject(),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    String server_response = response.toString();
                    try {
                        JSONObject json_object = new JSONObject(server_response);
                        JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));
                        for (int i = 0; i < json_array.length(); i++) {
                            Model_Account model_spinner = new Model_Account();
                            JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                            model_spinner.setName(json_arrayJSONObject.getString("CompanyName"));
                            model_spinner.setCompanyname(json_arrayJSONObject.getInt("AccountID"));
                            arrayobj_company_name.add(model_spinner);

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();

                }
            });

    //Creating request queue

    queue_company_name.add(jsonObjRequest_company_name);

    ArrayAdapter<Model_Account> company_names = new ArrayAdapter<Model_Account>(this, android.R.layout.simple_spinner_dropdown_item, arrayobj_company_name);
    spinner_company_name.setAdapter(company_names);
final Bundle extra = getIntent().getExtras();
if (extra != null) {

    id = extra.getInt("id");

    Cursor edit_accnt = account_sf_db.getData(id);
    if (edit_accnt.moveToFirst()) {
        do {


            compny_group.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.company_groups)));
            company_name.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Parent_company)));
            addrss1.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Address_line1)));
            address_2.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Address_line2)));
            address_3.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Address_line3)));
            pin_code.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Pincode)));
            land_line.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Landline1)));
            landline_2.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Landline2)));
            url.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Url)));
            email.setText(edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.Landline2)));
            String match = edit_accnt.getString(edit_accnt.getColumnIndexOrThrow(Model_Account.Company_name));
            Log.e("test",match);
        } while (edit_accnt.moveToNext());



    }
}

如何设置匹配适配器公司名称与此斗争可以有人帮助我!!

1 个答案:

答案 0 :(得分:0)

首先,使用适配器

获取要设置为微调器的字符串的位置
int position = spinnerAdapter.getPosition(json_arrayJSONObject.getString("CompanyName"));

之后,在位置

的微调器中选择
spinner.setSelection(position);

希望这适合你。