gpt4 book ai didi

android - 单击下一个 ExpandableListView 标题时,ExpandableListView 中的 EditText 内容消失

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:50 27 4
gpt4 key购买 nike

我在 expandablelistview 子项目中有一个 edittext。然后,我在这个 edittext 中输入了一个文本,但是每当我点击其他 expandablelistview 标题时,我输入的文本就会消失。

我已经在我的 Manifest.xml 中使用了 android:windowSoftInputMode="adjustPan" 和许多其他可能的修复,但没有奏效。

这是我的适配器:

public class ExpendableAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<ExpandItemModel>> _listDataChild;

public ExpendableAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<ExpandItemModel>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

final EditText etTotal = (EditText)convertView
.findViewById(R.id.et_total);

etTotal.setText(childText.getTotal);

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@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.list_group, null);
}



TextView tvHeader = (TextView) convertView
.findViewById(R.id.tv_header);
tvHeader.setTypeface(null, Typeface.BOLD);
tvHeader.setText(headerTitle);

return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

这是布局:

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rl_expen_but"
android:layout_below="@+id/rl_ats_draft2"
android:layout_above="@+id/selanjutnya"
>



<ExpandableListView
android:background="@color/cardview_light_background"
android:id="@+id/lvExp"
android:descendantFocusability="beforeDescendants"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:animateLayoutChanges="true"/>

<View
android:layout_below="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#bebebe"/>

</RelativeLayout>

我上面的代码有什么问题吗?谢谢。

编辑:

这是我按照 chandil03 的建议编辑代码后的样子。当我展开标题时,可扩展 ListView 不再刷新。但奇怪的是,当我在组位置 0 和子位置 0 的 edittext 中键入值时,然后在不同 goupposition 和 childposition 的其他一些 edittext 中出现我在先前的 edittext 中键入的相同值。或者更奇怪的是,以前的编辑文本中的值有时会消失。

static class ViewHolder {
protected View et;

public void addView(View et){
this.et = et;
}
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);

ViewHolder viewHolder;

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.addView(convertView
.findViewById(R.id.et_total));
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}

final EditText etTotal = (EditText)convertView
.findViewById(R.id.et_total);

etTotal.setText(childText.getTotal);

return convertView;
}

最佳答案

每当您展开另一个组时,列表就会刷新,并且您会在其中获得另一个没有文本的 EditText 实例。您在其中输入文本的 EditText 将成为另一个 groupView 的子项。

所以我建议您创建一个 ViewHolder 类并将您的 View 保存在其中并将其设置为标签,并在 getView() 中检查 convertView 是否为 null你已经完成了。只需从 convertView 添加 else 部分和 getTag 并相应地设置值。

例如:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_layout, parent, false);
ViewHolder holder = new ViewHolder(); // View holder to save views.
holder.addView(v.findViewById(R.id.myView));
v.setTag(holder);
}
else{
ViewHolder holder = (ViewHolder) v.getTag(); // get tag and set values
// Do whatever you need to with the group view
}
return v;
}

编辑 1

我已经修改了你的代码。另请查看评论。

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);
ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}

holder.editText.setText(childText.getTotal); // Here whatever you will type in edittext will be overwritten by the value of 'childText.getTotal'. So after you are done writing in edit text make sore you change that in "_listDataChild" list.

return convertView;
}

/**This is a class that holds view, Here i m not talking about support.v7.widget.RecyclerView.ViewHolder class. Though concept is more or less same.*/
class ViewHolder{
EditText editText;

public ViewHolder(View v)
{
editText = (EditText) v.findViewById(R.id.et_total);
}
}

编辑 2

getChildView() 方法中添加如下代码。

holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
childText.getTotal = holder.editText.getText().toString();
// In java variable contains reference of Object so when you change childText object, it will be reflected in same HashMap you are getting data from.

}
});

关于android - 单击下一个 ExpandableListView 标题时,ExpandableListView 中的 EditText 内容消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36594141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com