我想利用 expandlelistView 来显示产品和子产品

我想利用 expandlelistView 来显示产品和子产品,即 childHolder,在 childHolder 中,它包含一个 textView 和一个 Edittext,其中包含每个子产品的产品计数值,这里的问题是。

  1. 当我在编辑文本中输入值时,在折叠组时,数据会在编辑文本中丢失。

  2. 这些值在其他组edittext中重复。

3.如何保存数据以供以后在应用程序中使用。

下面的代码。

活动布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".ui.activities.ShelfCheckActivity">



<ExpandableListView

    android:id="@+id/listview"

    android:layout_width="wrap_content"

    android:layout_height="match_parent"

    android:dividerHeight="0dp"

    android:groupIndicator="@null"

    android:descendantFocusability="beforeDescendants"/>


</RelativeLayout>

适配器类


public class ShelfCheckAdapter extends BaseExpandableListAdapter {


ArrayList<ListItemModel> groupItem;

GroupViewHolder groupViewHolder;

ChildViewHolder childViewHolder;

Context context;

public LayoutInflater layoutInflater;


public ShelfCheckAdapter(ArrayList<ListItemModel> groupItem, Context 

context) {

    this.groupItem = groupItem;

    this.context = context;

}


public  void setInflater(LayoutInflater inflater)

{

    this.layoutInflater = inflater;

}


@Override

public int getGroupCount() {

    return groupItem.size();

}


@Override

public int getChildrenCount(int groupPosition) {

    return groupItem.get(groupPosition).getArrayList().size();

}


@Override

public Object getGroup(int groupPosition) {

    return groupItem.get(groupPosition);

}


@Override

public Object getChild(int groupPosition, int childPosition) {

    return groupItem.get(groupPosition).getArrayList().get(childPosition);

}


@Override

public long getGroupId(int groupPosition) {

    return groupPosition;

}


@Override

public long getChildId(int groupPosition, int childPosition) {

    return childPosition;

}


@Override

public boolean hasStableIds() {

    return false;

}


弑天下
浏览 70回答 1
1回答

慕的地8271018

只需更改以下方法并检查其工作与否!@Overridepublic View getChildView(int groupPosition, int childPosition, boolean&nbsp;&nbsp; &nbsp; isLastChild, View convertView, ViewGroup parent) {&nbsp; &nbsp; if(convertView == null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; convertView = LayoutInflater.from(context).inflate(R.layout.list_row_child,null);&nbsp; &nbsp; &nbsp; &nbsp; childViewHolder = new ChildViewHolder(convertView, groupPosition, childPosition);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; convertView.setTag(childViewHolder);&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; childViewHolder = (ChildViewHolder) convertView.getTag();&nbsp; &nbsp; }&nbsp; childViewHolder.childTitle.setText(groupItem.get(groupPosition)&nbsp;.getChildTitles().get(childPosition));&nbsp; &nbsp; &nbsp; return convertView;}另一个变化是private class ChildViewHolder {&nbsp; &nbsp; public TextView childTitle;&nbsp; &nbsp; public EditText et;&nbsp; &nbsp; public ChildViewHolder(View itemView, int groupPosition, int childPosition) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; childTitle = itemView.findViewById(R.id.textViewChild);&nbsp; &nbsp; &nbsp; &nbsp; et = itemView.findViewById(R.id.productCount);&nbsp; &nbsp; &nbsp; &nbsp; et.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(charSequence != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groupItem.get(groupPosition).getArrayList().get(childPosition).setValue(charSequence.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable editable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java