简单的适配器的问题,文字+图片的微调。 Java中,Android的适配器、文字、简单、问题

2023-09-05 07:26:05 作者:柠萌味的小可爱

大家好。 我有一个小问题......好吧,让我先状态,我想要完成的任务。 我有一个微调,拉弦出了存储阵列。 像这样,你不需要,虽然阅读:

Greetings all. 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 dont 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 radio button is not necessary. So the spinner should pop up and have a list:


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

到目前为止,我已经有了一个自定义的SimpleAdapter。  这是问题!:短信来了,但没有图像。继承人的code:

So far I've got a custom SimpleAdapter. Here is the Problem!! : the text comes up but not the image. Heres the code:


public class stageadapter extends SimpleAdapter {

private Context localContext;
private ArrayList> localList;

public stageadapter(Context context,
    ArrayList> 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;
}
}

我打算使用一个switch语句来设置不同的图像,每个名称。不过,我在这里停止,直到我能得到任何的图像显示。

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

我我如何打电话


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

            map = new HashMap();
            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

推荐答案

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

Remove the following line -- its confusing your adapter:

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;
        }

    }