我创建了一个可扩展的视图,其中包含每个组下的editText框。
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);
适配器代码:
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....
}
由于要回收视图的适配器功能,每当我在编辑框中键入一些文本时,当我向上和向下滚动时,此值也会丢失或复制到其他编辑框中。
在列表视图中,数组用于存储编辑框的值。
在可扩展列表视图中执行此操作的最佳方法是什么?数组数组? 您能为可扩展列表视图而不是列表视图提供示例吗?
编辑:试过这个,但我也有问题:
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是一个数组String [10] [10]。我的组是3,子组在组中最多7个,因此数组不小于我的数据。
答案 0 :(得分:1)
问题是TestWatcher。它会多次触发每种方法,但我还没有发现原因。因此,解决方案是使用FocusChangeListener或更好地显示alertDialog以填充该字段。然后在正面按钮上单击我将数据存储在数组中。
因此,通过替换TextWatcher,我设法解决了问题。