• Spinner的使用(一项内容)


    spinner_item1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/txt_name"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:textSize="16sp" />
    </LinearLayout>
    

    AdapterSpinner1.java

    public class AdapterSpinner1 extends BaseAdapter {
    
        private Context mContext;
        private List<String> mList;
    
        public AdapterSpinner1(Context pContext, List<String> pList){
            this.mContext = pContext;
            this.mList = pList;
        }
        @Override
        public int getCount() {
            return mList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.spinner_item1, null);
            if(convertView != null)
            {
                TextView txt_name = (TextView)convertView.findViewById(R.id.txt_name);
                txt_name.setText(mList.get(position));
            }
            return convertView;
        }
    }
    

    Activity中:

    1. 定义变量

        private Spinner mSpinnerZT;
        private ArrayList<String> listZT;
        private AdapterSpinner1 adpapter_ZT;
    

    2. 变量的初始化

    mSpinnerZT = (Spinner) findViewById(R.id.spinner_zt);
    
    listZT = new ArrayList<String>();
    adpapter_ZT = new AdapterSpinner1(this, listZT);
    mSpinnerZT.setAdapter(adpapter_ZT);
    

    3. 内容的动态改变

    listZT.clear();
     for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonData = jsonArray.getJSONObject(i);
        listZT.add(jsonData.getString("NAME"));
    }
    adpapter_ZT.notifyDataSetChanged();
    

    4. 取当前的选项

    final String sZT = listZT.get(mSpinnerZT.getSelectedItemPosition());
    

      

      

     

  • 相关阅读:
    node.js 与java 的主要的区别是什么
    基于Node.js+MySQL开发的开源微信小程序B2C商城(页面高仿网易严选)
    Linux下SVN安装配置
    Linux查看CPU和内存使用情况
    Linux添加/删除用户和用户组
    java远程调试
    springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
    客户端、服务端,跨域访问设置
    HTTP和HTTPS协议
    KMP算法代码
  • 原文地址:https://www.cnblogs.com/CipherLab/p/12288677.html
Copyright © 2020-2023  润新知