【发布时间】:2017-02-21 11:30:41
【问题描述】:
我有可扩展的列表视图,其中包含作为子项的 edittext,并且在此 ExpandableListView 下方当用户单击按钮时我有按钮,然后我想从 EditText 获取文本并存储在字符串中,我在 getChildView 方法中尝试了以下代码
我的适配器
public class ExListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> collections;
private ArrayList<ListDetails> listItem;
private TextView txtOk;
public ExListAdapter(Activity context, ArrayList<ListDetails> listItem,
Map<String, List<String>> collections, TextView txtOk) {
this.context = context;
this.collections = collections;
this.listItem = listItem;
this.txtOk = txtOk;
}
public Object getChild(int groupPosition, int childPosition) {
return 1;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Nullable
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, @Nullable View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_child, null);
}
etChild = (EditText) convertView.findViewById(R.id.etChild);
txtOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etChild.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Value : " + childPosition + etChild.getText().toString());
Toast.makeText(context, etChild.getText().toString() + "...", Toast.LENGTH_SHORT).show();
}
});
etChild.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
etChild.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Value : " + childPosition + etChild.getText().toString());
Toast.makeText(context, etChild.getText().toString() + "...", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
public int getChildrenCount(int groupPosition) {
return 1;
}
public Object getGroup(int groupPosition) {
return listItem.get(groupPosition).getValue();
}
public int getGroupCount() {
return listItem.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Nullable
public View getGroupView(final int groupPosition, boolean isExpanded,
@Nullable View convertView, ViewGroup parent) {
try {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group,
null);
}
TextView txtName = (TextView) convertView.findViewById(R.id.txtName);
RelativeLayout rlList = (RelativeLayout) convertView.findViewById(R.id.rlList);
txtName.setText(mArr.get(groupPosition).getValue());
} catch (Exception e) {
Logs.d("view- ExListAdapter", e, getClass().getSimpleName());
}
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void onGroupCollapsed(int groupPosition) {
try {
} catch (Exception e) {
}
super.onGroupCollapsed(groupPosition);
}
}
但是一直都是空白,请大家帮忙解决一下!!!
【问题讨论】:
-
你不是简单地得到这样的文本“etChild.getText().toString()”吗?没有“addTextChangedListener”?
-
我在吐司里试过了,但是显示为空!!!
-
我认为问题出在其他地方......你能分享你的适配器代码吗?
-
我更新了我的代码!!!
-
试试这个“etChild.getText().toString()”直接不带“addTextChangedListener”。
标签: android android-edittext expandablelistview