如何从自定义微调器获取ImageView的可绘制对象?

时间:2019-05-06 10:58:01

标签: java android adapter

我有一个带有ImageView和TextView的自定义微调器,但是返回代码不返回这些值之一,而是将其作为对象返回。

如何分别获得可绘制的id或textview字符串而不是微调器的对象?

我是否必须在适配器,微调器本身或其他组件中尝试获取数据?

Tq

这是我的Spinner 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="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/spinnerImage"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="5dp"
        android:layout_weight="1"
        android:src="@drawable/dark_red_square" /><!--Make sure image is present in Drawable folder-->

    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:textSize="14sp"
        android:gravity="center"
        android:text="Demo"
        android:textColor="#000" />
</LinearLayout>

此微调器处于片段状态。老实说,我不知道该在这里采样什么代码以供参考,但这是自定义适配器

public class CustomAdapter extends BaseAdapter {

    Context context;
    int flags[];
    String[] countryNames;
    LayoutInflater inflter;

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


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

    @Override
    public Object getItem(int position) {
        return countryNames[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflter.inflate(R.layout.spinner_item, null);
        TextView spinnerText= convertView.findViewById(R.id.spinnerText);
        ImageView spinnerImage = convertView.findViewById(R.id.spinnerImage);
        spinnerText.setText(countryNames[position]);
        spinnerImage.setImageResource(flags[position]);
        return convertView;
    }
}

0 个答案:

没有答案