Reputation: 1929
I created an expandable view that contains an editText box under each group.
public class ExpandableActivity extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable);
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
// preparing list data
//custom function that populates listDataHeader and listDataChild
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
Adapter Code:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.e_list_item, null);
}
TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);
vw_text_sub.setText(childText);
vw_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//store value?
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.e_list_group, null);
}
TextView vw_header = (TextView) convertView.findViewById(R.id.eheader);
vw_header.setTypeface(null, Typeface.BOLD);
vw_header.setText(headerTitle);
return convertView;
}
// other overridden methods here....
}
Because of the adapter functionality that is to recycle the views, whenever i type some text in an edit box this value is either lost or copied in other edit boxes too when i am scrolling up and down.
In list view an array is used to store values of edit boxes.
What is the best way to do it in an expandable list view? Array of arrays? Can you provide an example for an expandable list view and not a list view?
EDIT: Tried this but i also have problems:
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.e_list_item, null);
}
TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);
vw_text_sub.setText(childText);
if(tmp[groupPosition][childPosition] != null) {
vw_edit.setText(tmp[groupPosition][childPosition]);
}else{
vw_edit.setText("");
}
vw_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
tmp[groupPosition][childPosition] = arg0.toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
return convertView;
}
tmp is an array String[10][10]. My groups are 3 and children are max 7 in a group, so the array is not smaller than my data.
Upvotes: 0
Views: 545
Reputation: 1929
The problem is the TestWatcher. It fires each method a lot of times and i have not found out why. So the solution was to use FocusChangeListener or much better to show an alertDialog in order to fill the field. Then on positive button click i stored the data in the array.
So by replacing TextWatcher i managed to solve the problem.
Upvotes: 1