Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从自定义微调器获取可绘制的ImageView?_Java_Android_Adapter - Fatal编程技术网

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

Java 如何从自定义微调器获取可绘制的ImageView?,java,android,adapter,Java,Android,Adapter,我有一个带有ImageView和TextView的自定义微调器,但是返回代码不返回这些值中的任何一个,而是将其作为对象返回 如何分别获取可绘制id或textview字符串,而不是微调器的对象 我是否必须在适配器、微调器本身或其他试图获取数据的组件中执行此操作 Tq 以下是我的微调器XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.androi

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

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

我是否必须在适配器、微调器本身或其他试图获取数据的组件中执行此操作

Tq

以下是我的微调器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>

我所说的可提取是指未经编辑的可提取的id,但老实说,我不知道这是否足以满足您要获取可提取id的步骤?我希望调用活动从spinnerby drawable获取所选项目的可提取id我是指未经编辑的可提取的id,但老实说,我不知道这是否足以让您在哪一步获得可绘制的id?我希望调用活动从微调器获取所选项目的可绘制id
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;
    }
}