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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…