2017-02-13 6 views

答えて

0

テキストとImageViewのでカスタムアダプタを作成します。

その後
public class CustomSpinnerAdapter extends BaseAdapter { 
Context context; 
int image[]; 
String[] text; 
LayoutInflater inflter; 

public CustomAdapter(Context applicationContext, int[] image, String[] text) { 
    this.context = applicationContext; 
    this.image = image; 
    this.text = text; 
    inflter = (LayoutInflater.from(applicationContext)); 
} 

@Override 
public int getCount() { 
    return flags.length; 
} 

@Override 
public Object getItem(int i) { 
    return null; 
} 

@Override 
public long getItemId(int i) { 
    return 0; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    view = inflter.inflate(R.layout.spinner_item_layout, null); 
    ImageView icon = (ImageView) view.findViewById(R.id.spImageView); 
    TextView names = (TextView) view.findViewById(R.id.spTextView); 
    icon.setImageResource(image[i]); 
    names.setText(text[i]); 
    return view; 
} 

}

スピナーにこのアダプタを設定する:ここで

CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, YourImageArray, YourTextArray); 
     spinner.setAdapter(adapter); 

は、各スピナー項目(spinner_item_layoutのレイアウトです。 xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/spTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <ImageView 
      android:id="@+id/spImageView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:contentDescription="@string/app_name"/>" 

</LinearLayout> 

これが役に立ちます。 :)

+0

私は緑の手ですので、レイアウトの違いは何ですか?spinner_value_layoutとspinner_item_layout.in言い換えれば、このレイアウトは何ですか?私はpuzzed.er ..... .. –

+0

私は答えを編集しました。これは以前より簡単です。フィードバックをお願いします。 – tahsinRupam

+0

うん、私は作ったよ。役に立つ.Cuzはこのウェブサイトの仕組みで、私は15の評判を得るまであなたに投票しない。 –

関連する問題