我该怎么用HashMap BaseAdapter [英] how can I use HashMap for BaseAdapter

查看:140
本文介绍了我该怎么用HashMap BaseAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来,我使用的SimpleAdapter HashMap中,像这样的:

Originally, I'm using hashmap using SimpleAdapter, like this:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
int leng = nodes.getLength();
for (int i = 0; i < leng; i++) {
    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nodes.item(i);
    map.put("nama", parser.getValue(e, "nama"));
    map.put("in", parser.getValue(e, "in"));
    mylist.add(map);
    }
// Adding myList to ListView
ListAdapter adapter = new SimpleAdapter(LihatFarmasiObat.this, mylist,
R.layout.list_farmasi_obat, new String[] { "nama", "in"  }, 
new int[] {R.id.txtListFarmasiNama, R.id.txtListFarmasiIn});

listFarmasiObat.setAdapter(adapter);

但现在我试图把里面的EditText的ListView,我得到了的这里
我想,code和它的工作原理,(我需要改变一些,但code工作)。
而但是当我试图把它与我自己的code结合起来,我得到了一个错误的无法从HashMap中转换为LihatFarmasiObat.ListItem 这些行:

holder.caption.setText(((ListItem)mylist.get(position)).caption);
//It got an error on mylist

holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus){
            final int position = v.getId();
            final EditText Caption = (EditText) v;
            ((ListItem)mylist.get(position)).caption = Caption.getText().toString();
            // it also got same error on mylist.
        }
    }
});
return convertView;

class ListItem {
    String caption;
    //this is the problem (I think) I don't know how to make this hashmap
}

我已经尝试将其更改为任何其他方式,但它不工作。

I already try to change it to any other way but it's not working.

这是我的全code:

public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public MyAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        new LoadFarmasi().execute(); // This is for filling mylist with hashmap
        notifyDataSetChanged();
    }

    public int getCount() {
        return mylist.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_farmasi_obat, null);
            holder.caption = (EditText) convertView.findViewById(R.id.editText1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //Fill EditText with the value you have in data source

        holder.caption.setText(((ListItem)mylist.get(position)).caption);
        holder.caption.setId(position);

        //we need to update adapter once we finish with editing
        holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    ((ListItem)mylist.get(position)).caption = Caption.getText().toString();
                }
            }
        });
        return convertView;
    }
}

class ViewHolder {
    EditText caption;
}
class ListItem {
    String caption;
}

有人能帮助我吗?我与这场斗争了几天

Can someone help me? I'm struggle with this for a few days

推荐答案

getView 你需要使用

 HashMap<String,String> map =mylist.get(position);
 // position gives you the index
 String value = map.get("nama");
 String value2 = map.get("in");

现在使用String的,并据此设置意见

Now use the String's and set it to views accordingly

您的HashMap的ArrayList

You have arraylist of hashmap.

这篇关于我该怎么用HashMap BaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆