Eugene
Eugene

Reputation: 60204

How shall I iterate in BaseAdapter getView() if I use HashMap

I have an extended from BaseAdapter class which used as a custom Adapter for my ListView. If I use List<> as a data set I don't have any troubles in getView() method and everything is ok - my list is populated with all data from the List. But if I would use HashMap this never works. As far as I understand getView() iterates through collection and this works fine with List because it is iterable.

private class MAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                v = mInflater.inflate(R.layout.list_item, null);
            }

            Task task = taskList.get(position); // works perfect if taskList is List and doesn't work if it is HashMap. What shell I do to use HashMap here?

            if (task != null) {
                TextView tvDescription = (TextView) v
                        .findViewById(R.id.task_text_view);
                TextView tvTime = (TextView) v
                        .findViewById(R.id.time_text_view);

                if (tvDescription != null) {
                    tvDescription.setText(task.getDescription());
                }

                if (tvTime != null) {
                    tvTime.setText(task.showTime());
                }
            }

            return v;
        }

Upvotes: 2

Views: 2987

Answers (1)

Robby Pond
Robby Pond

Reputation: 73494

You can't. BaseAdapter implements ListAdapter and it's called ListAdapter for a reason. Why do you want to use a HashMap?

Update:

Remove from the list.

for(int j = list.size() - 1; j >= 0; j--){
  Object o = list.get(j);
  if(o.getId().equals(id)) {
    list.remove(o); // find obj and remove
    break;
  }
}
adapter.notifyDataSetChanged(); // update ListView

or Keep both a list and HashMap. I recommend just using the List.

Object o = map.remove(id); // remove object by id
list.remove(o); // remvove object from list
adapter.notifyDataSetChanged(); // update ListView

Upvotes: 2

Related Questions