Android开发:自定义Spinner适配器
来源:互联网 发布:天天回程车软件 编辑:程序博客网 时间:2024/06/05 15:00
一.大致效果
~别嫌它丑
二.关键代码
在注释中讲重点吧。
(1)Spinner的布局: car_brand_spinner.xml
即为弹出来的下拉列表的布局啦,后面的那个布局就不拿出来丢人现眼了,反正知道有一个Spinner的id为carBrandSpinner就可以了。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/car_brand_name" android:layout_gravity="left" android:layout_alignParentLeft="true" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/car_brand_flag" android:layout_gravity="right" android:layout_alignParentRight="true" /> </RelativeLayout></LinearLayout>
(2)适配器
ArrayAdapter carBrandAdapter=new ArrayAdapter<String>( AddCarActivity.this, android.R.layout.simple_spinner_dropdown_item, carBrandNameList//是String[],就是所有要显示的brandName){ @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { convertView = View.inflate(AddCarActivity.this,R.layout.car_brand_spinner,null);//获得Spinner布局View if(convertView!=null) { TextView carBrandNameView = (TextView)convertView.findViewById(R.id.car_brand_name); ImageView carBrandFlagView = (ImageView)convertView.findViewById(R.id.car_brand_flag); try { JSONObject json = new JSONObject(carBrandList.get(position).get("carBrand").toString()); carBrandNameView.setText(json.getString("carBrandName"));//设置数据,我这里的数据是从服务器读出来的,所以前面有一个转化取值的过程 }catch (Exception e){} Bitmap bitmap =Common.String2Bitmap(carBrandList.get(position).get("carBrandFlagContent").toString());//这里也一样,图片数据来自于服务器,同时有一个将数据从String转Bitmap的过程 if(bitmap!=null) carBrandFlagView.setImageBitmap(bitmap);//显示图片 } return convertView; }};//给Spinner set适配器Spinner carBrandSpinner=(Spinner)findViewById(R.id.carBrandSpinner);carBrandSpinner.setAdapter(carBrandAdapter);carBrandSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ @Override//重写Item被选择的事件 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {} @Override public void onNothingSelected(AdapterView<?> parent) {}});
到此结束!
0 0
- Android开发:自定义Spinner适配器
- Android开发适配器系列:Spinner使用自定义适配器
- Spinner使用自定义适配器
- Android 开发之自定义 Spinner
- Android开发适配器系列:Spinner绑定ArrayAdapter适配器
- android开发 AutoCompleteTextView自定义适配器
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- Android 实现自定义Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- 自定义Android的Spinner
- android自定义Spinner
- Android 实现自定义Spinner
- Android自定义Spinner
- android之自定义Spinner
- httpSession的正确理解
- 第一章 JAVA入门(Eclipse续集)
- LeetCode Binary Tree Postorder Traversal
- 《UNIX环境高级编程》(exec)
- ldpack工作日记-2016/5/11&12
- Android开发:自定义Spinner适配器
- App引导页面代码示例
- strcpy函数实
- 全局捕获异常
- 微软面试100题-66
- poj 2586 Y2K Accounting Bug
- 使用HTML + CSS实现圆圈背景下的数字
- 大二实习投简历小记
- AOJ-737 关于最大公约数的疑惑