简单适配器问题,微调文本图像. Java,Android

前端之家收集整理的这篇文章主要介绍了简单适配器问题,微调文本图像. Java,Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问候全部
我有一点问题吗?嗯,让我先说明我要完成什么.
我有一个微调器从存储的数组中拉出字符串.
像这样,你不需要阅读它:
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);

简单而又足够.我想添加一个图像到微调器..单选按钮是不必要的.所以微调器应该弹出并列出一个列表:

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

到目前为止,我有一个自定义的SimpleAdapter.
这是问题! :文本出现,但不是图像.代理代码

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,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语句来为每个名称设置不同的图像.但是我停在这里,直到我能看到任何图像来显示.

我打电话

   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(),R.layout.stagerow,new String[] {
                                                 "Name","Icon"},new int[] {
                                                     R.id.stage_name,R.id.stage_icon });
           mStageSpin.setAdapter(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,"Two");
        map.put("Icon",R.drawable.icon);
        list.add(map);

        Spinner spin = (Spinner) findViewById(R.id.spin);
        myAdapter adapter = new myAdapter(getApplicationContext(),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[] to) {
            super(context,data,to);

        }

        @Override
        public View getView(int position,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;
        }

    }
原文链接:https://www.f2er.com/java/122236.html

猜你在找的Java相关文章