學習Android自訂Spinner適配器_Android

來源:互聯網
上載者:User

本文為大家分享Android自訂Spinner適配器的相關知識點,供大家參考,具體內容如下

一、大致效果

二.關鍵代碼

在注釋中講重點吧。
(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) {}});

到此結束!

以上就是關於Android Spinner適配器的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.