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

      

      

     

  • 相关阅读:
    C#线程类Thread初步
    无限级分类存储过程版
    C#多线程编程实例实战
    数据库里阻塞和死锁情况 看那里死锁的存储过程
    预防按钮的多次点击 恶意刷新
    .net2.0文件压缩/解压缩
    HttpHandler和HttpModule入门
    反射,枚举,绑定下拉框
    在C#里关于定时器类
    判断上传的图片文件格式是否合法不是用后缀做的判断
  • 原文地址:https://www.cnblogs.com/CipherLab/p/12288677.html
Copyright © 2020-2023  润新知