Android:获取Spinner数据到字符串

时间:2016-04-04 08:35:58

标签: android spinner

我想获得字符串的微调值。我已经使用自定义微调器适配器将数据从json和数据加载成功添加到微调器,但我无法获取加载的数据 字符串。

import re
p = re.compile(ur'(class .*?\(.*?)(?:[\s,]+Bar\s*|\s*Bar[\s,]+)')
test_str = u"class Foo(Bar, Baz, Jazz):\n   pass\n\n\n\nclass Foo(Baz, Bar, Jazz):\n   pass\n\nclass Foo(Baz, Jazz, Bar):\n   pass"
subst = u"\1"

result = re.sub(p, subst, test_str)

这是我的自定义Spinner适配器。

public class RegisterActivity extends Activity { 
    private Spinner sprCity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        getId();
        addSpinnerData();
        setListner();   
    }
    private void getId() 
    {
        sprCity = (Spinner) findViewById(R.id.sprCity);
    }
    private void setListner() {
            sprCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String text= parent.getItemAtPosition(position).toString() ;
                    Log.e("Spinner detail ",">>>>>>"+text);
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
    }
    private void addSpinnerData() {
    JSONArray cityArray = object.getJSONArray(WebField.ZIP_DETAIL.RESPONSE_ARRAY_CITY_LIST);
    for (int i = 0; i < cityArray.length(); i++) 
    {
        getZipDetailModel = new GetZipDetailModel();
        JSONObject object1 = cityArray.getJSONObject(i);
        Log.e("Checking ojbect1", "******" + object1);
        strGetCity = object1.getString(WebField.ZIP_DETAIL.RESPONSE_OBJECT_CITY_NAME);
        getZipDetailModel.setStrCity(object1.getString(WebField.ZIP_DETAIL.RESPONSE_OBJECT_CITY_NAME));
        alZilcode.add(getZipDetailModel);
        myAdapter = new MyAdapter(RegisterActivity.this, alZilcode);
    }
        sprCity.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();       
    }
}

这是我的模特课。

        public class MyAdapter extends BaseAdapter{
    private Context context;
    private static LayoutInflater inflater;
    private ArrayList<GetZipDetailModel> objects;
    public MyAdapter(Context context, ArrayList<GetZipDetailModel> p1) {

        this.context = context;
        this.objects = p1;
    }
    @Override
    public int getCount() {
        return objects.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    class Holder
    {
        TextView lable;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View myView = null;
        try {
            Holder holder;
            myView = convertView;
            if (myView == null) {
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                myView = inflater.inflate(R.layout.adapter_city, null);
                holder = new Holder();
                holder.lable= (TextView) myView.findViewById(R.id.tvCityName);
                myView.setTag(holder);
            }
            else {

                holder = (Holder) myView.getTag();
            }
            holder.lable.setText(objects.get(position).getStrCity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myView;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

 @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

            String text= (String) sprCity.getSelectedItem();
            Log.e("Spinner detail ",">>>>>>"+text);
    }

答案 1 :(得分:0)

最后我得到了解决方案。

 sprCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



                    Log.e("Checking spinner", ">>>" + alZilcode.get(position).getStrCity().toString());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });