如何在微调器项目上添加图像?

时间:2012-10-11 13:01:05

标签: android android-spinner

如何在Android上的微调器上放置图像图标请帮助我这是我的spiner代码如何在列表中的每个名称上添加图像?我想在微调器上的eact文本上显示图像

            Spinner sp = (Spinner) findViewById(R.id.lgnspinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.network_array,
                    android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            sp.setAdapter(adapter);

            sp.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0) {
                        // text}

                        if (position == 1) {

                            // text

                        }
                        if (position == 2)

                        {
                            // text
                        }
                        if (position == 3) {
                            // text
                        }
                    }
                }
            });

XML:

<Spinner
    android:id="@+id/lgnspinner"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="145dip"
    android:layout_height="wrap_content"
    android:prompt="@string/network_prompt"
    android:textColor="#000000" />

<string-array name="network_array" >

    <item>
    GPRS
    </item>

    <item>
    Wifi
    </item>

    <item>
    OtherNetwork
    </item>

    <item>
    SMS
    </item>
</string-array>

2 个答案:

答案 0 :(得分:7)

enter image description here

我们将展示如何使用SimpleCursorAdapter和ViewBinder向Android微调器添加图标。

// get the spinner widget
mSpinner = (Spinner) findViewById(R.id.spinner1);
...
// fetch data from db. query might look something like this:
// SLECT _id, name FROM ....
mCursor = mDb.getData();
String[] from = new String[]{"_id", "name"};
int[] to = new int[]{R.id.icon, R.id.text};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.spinner_entry, mCursor, from, to);
adapter.setViewBinder(new SpinnerViewBinder());
mSpinner.setAdapter(adapter);

spinner条目的布局spinner_entry.xml文件可能如下所示:

<!--?xml version="1.0" encoding="utf-8"?-->

<LinearLayout ...>

    <ImageView android:id="@+id/icon"   .../>

    <TextView android:id="@+id/text"   .../>

</LinearLayout>

关键在于SpinnerViewBinder.java

public class SpinnerViewBinder implements SimpleCursorAdapter.ViewBinder {

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch (viewId) {
case R.id.text:
    // the textview
    TextView mView = (TextView) view;
    // display the name
    mView.setText(cursor.getString(columnIndex));
    break;

case R.id.icon:
    // the icon
    ImageView mIconView = (ImageView) view;
    int dialectId = cursor.getInt(columnIndex);
    switch (dialectId){
    case 0:
        mIconView.setImageResource(R.drawable.x1);
        break;
    case 1:
        mIconView.setImageResource(R.drawable.x2);
        break;
    default:
        mIconView.setImageResource(R.drawable.x3);
        break;
    }
}
return true;
}
}

参考下载这个简单的应用程序:     http://www.ziddu.com/download/12964268/AndroidCustomSpinner_files_101214a.zip.html

答案 1 :(得分:3)

请参阅:Customizing a spinner in android.

希望它适合你。