无法在CustomAdapter

时间:2016-03-22 16:39:32

标签: android listview android-arrayadapter

我为HashMap制作了自定义适配器,问题是我无法使用HashMap设置CustomAdapter的TextView。获取HashMap位置的第二行最后一行获取语法错误。 请指导我如何解决语法错误并设置CustomAdapter。

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {

        private SparseBooleanArray mSelectedItemsIds;
        private LayoutInflater inflater;
        private Context mContext;
        private List<HashMap<String, Object>> list;
        Blocklist blocklist;

        public CustomAdapter (Context context, int resourceId, List<HashMap<String, Object>> list) {
            super(context, resourceId, list);
            mSelectedItemsIds = new SparseBooleanArray();
            mContext = context;
            inflater = LayoutInflater.from(mContext);
            this.list = list;
        }

        private static class ViewHolder {
            TextView itemName;
        }

        public View getView(int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.custom_textview, null);
                view = inflater.inflate(R.layout.custom_textview, null);
                holder.itemName = (TextView) view.findViewById(R.id.custom_tv);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.itemName.setText(list.get(position));
            return view;
        }

Blocklist.java

HashMap<String,Object> hm = new HashMap<String,Object>();
                hm.put(ID, cursor.getLong(0));
                hm.put(ORIGINATING_ADDRESS, cursor.getString(1));
                hm.put(MESSAGE_BODY, cursor.getString(2));
                arrayList.add(hm);
                cursor.moveToNext();

2 个答案:

答案 0 :(得分:2)

你有:

list.get(position)

上述行无效,因为 holder.itemName.setText((String)list.get(position).get(MESSAGE_BODY)); 会导致Hashmap对象而不是字符串对象。如果你想要示例来显示MESSAGE_BODY,你必须像这样使用它

view = inflater.inflate(R.layout.custom_textview, null);

你也不需要膨胀

Fetch<CommonCalendarEvent, CommonCalendarEventPart> evf

twice`

答案 1 :(得分:0)

list.get(position)

返回HashMap中的对象position,而不是String

相关问题