java 微调器中的 SimpleAdapter、文本和图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3688925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:01:22  来源:igfitidea点击:

SimpleAdapter, Text and Image in spinner

javaandroid

提问by Brian

I've got a little problem. Well, let me first state what I'm trying to accomplish. I had a spinner that pulls strings out of a stored array. Like so, you don't need to read it though:

我有一个小问题。好吧,让我首先说明我要完成的任务。我有一个微调器,可以从存储的数组中拉出字符串。像这样,你不需要阅读它:

ArrayAdapter<?> healthadapter = ArrayAdapter.createFromResource(this, R.array.health, android.R.layout.simple_spinner_item);
mHealthSpin = (Spinner) findViewById(R.id.health_spin);
healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mHealthSpin.setAdapter(healthadapter);

Simple and almost sufficient. I would like to add an image to the Spinner.the RadioButtonis not necessary. So the Spinnershould pop up and have a list:

简单且几乎足够。我想将一个图像添加到Spinner.RadioButton是没有必要的。所以Spinner应该弹出并有一个列表:

TEXT     *IMAGE*
TEXT2    *IMAGE*
TEXT3    *IMAGE*

So far I've got a custom SimpleAdapter.Here is the Problem!!:

到目前为止,我已经有了一个习惯,SimpleAdapter.这是问题所在!!

The text comes up but not the image.

文本出现,但不是图像。

Here is the code:

这是代码:

public class stageadapter extends SimpleAdapter {

    private Context localContext;
    private ArrayList<HashMap<String, Object>> localList;

    public stageadapter(Context context, ArrayList<HashMap<String, Object>> list, int resource, String[] from, int[] to) {
        super(context, list, resource, from, to);
        localContext = context;
        localList = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            LayoutInflater inflater = (LayoutInflater) localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.stagerow, null);
        }
        TextView name = (TextView) convertView.findViewById(R.id.stage_name);
        name.setText((String) localList.get(position).get("Name"));
        ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon);

        icon.setImageResource(R.drawable.icon);

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            LayoutInflater inflater = (LayoutInflater) localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.stagerow, null);
        }
        TextView name = (TextView) convertView.findViewById(R.id.stage_name);
        name.setText((String) localList.get(position).get("Name"));
        ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon);
        icon.setImageResource(R.drawable.icon);

        return convertView;
    }
}

I plan to use a switchstatement to set different images to each name. however I stopped here until I can get any image to show.

我计划使用一个switch语句为每个名称设置不同的图像。但是我在这里停了下来,直到我可以显示任何图像为止。

How i am calling:

我如何打电话:

ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("Name", "One");
    map.put("Icon", R.drawable.icon);
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("Name", "Two");
    map.put("Icon", R.drawable.icon);
    list.add(map);

    mStageSpin = (Spinner) findViewById(R.id.stage_spin);
    stageadapter adapter = new stageadapter(getApplicationContext(), list, R.layout.stagerow, new String[]{"Name", "Icon"}, new int[]{R.id.stage_name, R.id.stage_icon});
    mStageSpin.setAdapter(adapter);

The answer for me is in the comments.

我的答案在评论中。

回答by Sameer Segal

Remove the following line -- its confusing your adapter:

删除以下行——它混淆了你的适配器:

healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Name", "One");
        map.put("Icon", R.drawable.icon);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("Name", "Two");
        map.put("Icon", R.drawable.icon);
        list.add(map);

        Spinner spin = (Spinner) findViewById(R.id.spin);
        myAdapter adapter = new myAdapter(getApplicationContext(), list,
                R.layout.list_layout, new String[] { "Name", "Icon" },
                new int[] { R.id.name, R.id.icon });

        spin.setAdapter(adapter);

    }

    private class myAdapter extends SimpleAdapter {

        public myAdapter(Context context, List<? extends Map<String, ?>> data,
                int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_layout,
                        null);
            }

            HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);

            ((TextView) convertView.findViewById(R.id.name))
                    .setText((String) data.get("Name"));
            ((ImageView) convertView.findViewById(R.id.icon))
                    .setImageResource(R.drawable.icon);

            return convertView;
        }

    }

回答by Vicky Kapadia

The problem is in getView you have assigned text corresponding to position using

问题是在 getView 中,您使用了与位置相对应的文本

((TextView) convertView.findViewById(R.id.name)) .setText((String) data.get("Name"));

((TextView) convertView.findViewById(R.id.name)) .setText((String) data.get("Name"));

But for image u have used the same resource i.e.

但是对于图像,您使用了相同的资源,即

((ImageView) convertView.findViewById(R.id.icon)) .setImageResource(R.drawable.icon);

((ImageView) convertView.findViewById(R.id.icon)) .setImageResource(R.drawable.icon);

You need to use the "data" hashmap list and assign the image here

您需要使用“数据”哈希图列表并在此处分配图像

回答by Asiimwe

Yes @Vicky you are right. For image it should be

是的@Vicky 你是对的。对于图像,它应该是

((ImageView) convertView.findViewById(R.id.icon)) .setBackgroundResource((Integer) data.get("Icon"));

回答by Neil Townsend

Based on the above answer, but with a few tweaks to simplify the code and make it work for the dropdown as well. First define the xml for the row layout you want. In this case a textview and a image in a row:

基于上述答案,但进行了一些调整以简化代码并使其也适用于下拉列表。首先为您想要的行布局定义 xml。在这种情况下,文本视图和图像连续:

text_image_spinner_layout.xml

text_image_spinner_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView android:id="@+id/text_image_spinner_text_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView android:id="@+id/text_image_spinner_image_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:src="@drawable/icon" />
</LinearLayout>

Then, create the class that will build each row as needed:

然后,创建将根据需要构建每一行的类:

TextImageAdapter.java

文本图像适配器.java

public class ImageTextAdapter extends SimpleAdapter {
public final static String keyForTextInAdapter  = "text";
public final static String keyForImageInAdapter = "image";

private final static String fromKeys[]        = {keyForTextInAdapter, keyForImageInAdapter};
private final static int    toIds[]           = {MyR.id.text_image_spinner_text_tv, MyR.id.text_image_spinner_image_imageview};
private final static int    TO_ID_TEXT_INDEX  = 0;
private final static int    TO_ID_IMAGE_INDEX = 1;
private final static int    layoutId          = MyR.layout.text_image_spinner_layout;

private final static int    TYPE_SHOWN_SELECTION = 0;
private final static int    TYPE_IN_DROPDOWN     = 1;

LayoutInflater inflater;
private List<? extends Map<String, ?>> dataIn;

public ImageTextAdapter(Context context, List<? extends Map<String, ?>> data) {
    super(context, data, layoutId, fromKeys, toIds);
    inflater  = LayoutInflater.from(context);
    dataIn    = data;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return makeView(position, convertView, parent,TYPE_IN_DROPDOWN);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return makeView(position, convertView, parent, TYPE_SHOWN_SELECTION);
}

private View makeView(int position, View convertView, ViewGroup parent, int type) {
    // Use the int type if you want to determine different actions if you have
    // differing requirements for the dropdown and the shown selection.
    if (convertView == null) convertView = inflater.inflate(layoutId, null);

    HashMap<String, Object> data = (HashMap<String, Object>)dataIn.get(position);

    ((TextView) convertView.findViewById(toIds[TO_ID_TEXT_INDEX])).setText((String) data.get(keyForTextInAdapter));
    ((ImageView) convertView.findViewById(toIds[TO_ID_IMAGE_INDEX])).setImageBitmap((Bitmap) data.get(keyForImageInAdapter));

    return convertView;
  }
}

And now you just need to build the spinner adapter when instantiating it. You provide the "things" ...:

现在你只需要在实例化它时构建微调适配器。您提供“东西”...:

private void setUpAppOptions() {
    ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map;

    for (int i=0; i<things.length; i++) {
        map = new HashMap<String, Object>();
        map.put(ImageTextAdapter.keyForTextInAdapter, things[i].name);
        map.put(ImageTextAdapter.keyForImageInAdapter, things[i].image);
        list.add(map);
    }

    ImageTextAdapter adapter = new ImageTextAdapter(myContext, list);
    appOptionSpinner.setAdapter(adapter);
}