Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

android - how can I use HashMap for BaseAdapter

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

But now I'm trying to put a EditText inside ListView, and I got this code from here. I tried that code and it works, (I need to change some but the code is working). and but when I tried to combine it with my own code, I got an error Cannot cast from HashMap to LihatFarmasiObat.ListItem on these line:

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.

and this is my full 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In getView you need to use

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

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

You have arraylist of hashmap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...