It seems simple, but for the life of me I cannot understand it.
I have an ExpandableListView with a custom ExpandableListAdapter that came from BaseExpandableListAdapter. When I add a new element, I just want to focus the new element. I can't get this to work.
I tried the following, which does not work. This applies to list activity:
private void processNewItem(C360ItemOwner itemOwner, int pos){ Item item = itemOwner.newItem(pos); expAdapter.notifyDatasetChanged(); expandList.requestFocus(); if (item.getParentType() == Item.PARENT_IS_CHECKLIST) {//main level expandList.setSelectedGroup(pos); } else //Item.PARENT_IS_ITEM //sub level expandList.setSelectedChild(item.getParentAsItem().getPosition(), pos, true); }
I tried to do this in the adapter of the getView method, which does not work:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) convertView = inf.inflate(R.layout.edit_group_item, null); Item group = (Item) getGroup(groupPosition); convertView.setTag(group); EditText edtParent = (EditText) convertView.findViewById(R.id.edtParent); edtParent.setTag(convertView); scatterItemText(edtParent); if (group.isNew()) { Toast.makeText(edtParent.getContext(), "Found Focus Item " + groupPosition, Toast.LENGTH_SHORT).show(); edtParent.requestFocus(); } return convertView; }
I tried to configure a couple of fields in the adapter: focusItem, the Item object is write-only and focusEdit is a read-only EditText obect. Therefore, I assign the element to getView, it saves the editing in the field. I call it this way:
private void processNewItem(C360ItemOwner itemOwner, int pos){ Item item = itemOwner.newItem(pos); expAdapter.setFocusItem(item);
Then in the getView of the adapter:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) convertView = inf.inflate(R.layout.edit_group_item, null); Item group = (Item) getGroup(groupPosition); convertView.setTag(group); EditText edtParent = (EditText) convertView.findViewById(R.id.edtParent); edtParent.setTag(convertView); scatterItemText(edtParent); if (group == focusItem) { Toast.makeText(edtParent.getContext(), "Found Focus Item " + groupPosition, Toast.LENGTH_SHORT).show(); focusEdit = edtParent; } return convertView; }
I really hope for this last method, but this line:
EditText edtFocus = expAdapter.getFocusEdit();
Occurs before the getView () method is run, which would assign it (therefore it is always null)! :-( plz halp
EDIT: I know this will be messy, but what if I executed a thread to wait a few hundred milliseconds so that getView () could start up and then try to get focusEdit?