【问题标题】:Issue with setting text to edittext inside Expandablelistview在 Expandablelistview 中将文本设置为 edittext 的问题
【发布时间】:2015-01-14 08:31:03
【问题描述】:

我在可扩展列表视图中的每个最后一个子项中都使用了 edittext。 Edittexts are visible only if its checkbox is selected.这工作正常,但问题是当我选择一个新的 lastchild 时,以前的 edittext,s 值被复制到这个。此外,当我滚动时,每个孩子的 edittext 值都会互换,这给我带来了很多麻烦。如何解决这个问题?我尝试了很多方法。不幸的是,他们都没有工作。我在这里发布 getchildView 的代码。在处理编辑文本的可见性时我也没有任何问题。

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

    final int groupPos=groupPosition;
    final int childPos=childPosition;
    final boolean islstchild=isLastChild;
    final Industries_Level_3 child = (Industries_Level_3) getChild(groupPosition, childPosition);

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.multilevel_list_item_child, null);
    }

    final EditText edtOther=(EditText)convertView.findViewById(R.id.editIndOther);



    final CheckBox chkSelect=(CheckBox)convertView.findViewById(R.id.checkBoxInd);
    chkSelect.setText(child.getIndname());

    if(child.getSelected()!=null)
    {
    chkSelect.setChecked(child.getSelected());

    if (isLastChild) {
        if (child.getSelected()==true) {            
            edtOther.setVisibility(View.VISIBLE);

        } else {
            edtOther.setVisibility(View.GONE);
        }
    }else
    {
        edtOther.setVisibility(View.GONE);
    }
    }

    /*Setting text to edittext*/
    if(child.getOtherText()!=null)
    {
        if(child.getOtherText().length()>0)
        {
            edtOther.setText(child.getOtherText().toString());
        }
    }
    chkSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(chkSelect.isChecked())
            {
                if(islstchild)
                {
                    edtOther.setText("");
                    edtOther.setVisibility(View.VISIBLE);
                    edtOther.requestFocus();

                }
                    grouplist.get(groupPos).getIndustry_Based_On_Group().get(childPos).setSelected(true);
                notifyDataSetChanged();

                if(!islstchild)
                {
                    int count=0;
                    for (int i = 0; i < grouplist.get(groupPos).getIndustry_Based_On_Group()
                        .size(); i++) {
                    if (grouplist.get(groupPos).getIndustry_Based_On_Group().get(i).getSelected()==true) {

                        if(i!=grouplist.get(groupPos).getIndustry_Based_On_Group().size()-1)
                        {
                        count = count + 1;
                        }

                        if (count == grouplist.get(groupPos).getIndustry_Based_On_Group().size()-1) {
                            grouplist.get(groupPos).setSelected(true);
                            notifyDataSetChanged();
                        }
                    }

                }   
                }


            }else
            {
                if(!islstchild)
                {
                grouplist.get(groupPos).setSelected(false);
                notifyDataSetChanged();
                }else
                {
                    edtOther.setText("");
                    child.setOtherText("");
                    edtOther.setVisibility(View.GONE);

                }
                grouplist.get(groupPos).getIndustry_Based_On_Group().get(childPos).setSelected(false);
                notifyDataSetChanged();

            }
        }
    });


    edtOther.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            String text= edtOther.getText().toString();
            child.setOtherText(text);
        }

        @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

        }
    });

    return convertView;
}  

更新:完整代码

public class IndBussinessAdapter extends BaseExpandableListAdapter{

LayoutInflater inflater;

/* list of group */
private List<IndustryGroup_Level_2> grouplist;

public IndBussinessAdapter(Context context,
        List<IndustryGroup_Level_2> bsgrouplist) {
    super();
    this.grouplist = bsgrouplist;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public int getChildrenCount(int groupPosition) {
    List<Industries_Level_3> ch = grouplist.get(groupPosition)
            .getIndustry_Based_On_Group();
    return ch.size();
}

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

    final int groupPos=groupPosition;
    final int childPos=childPosition;
    final boolean islstchild=isLastChild;
    final Industries_Level_3 child = (Industries_Level_3) getChild(groupPosition, childPosition);

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.multilevel_list_item_child, null);
    }

    final EditText edtOther=(EditText)convertView.findViewById(R.id.editIndOther);

    /*Setting text to edittext*/
    if(child.getOtherText()!=null)
    {
        if(child.getOtherText().length()>0)
        {
            edtOther.setText(child.getOtherText().toString());
        }
    }

    final CheckBox chkSelect=(CheckBox)convertView.findViewById(R.id.checkBoxInd);
    chkSelect.setText(child.getIndname());

    if(child.getSelected()!=null)
    {
    chkSelect.setChecked(child.getSelected());

    if (isLastChild) {
        if (child.getIndname().startsWith("Other") && child.getSelected()==true) {          
            edtOther.setVisibility(View.VISIBLE);
            edtOther.requestFocus();

        } else {
            edtOther.setVisibility(View.GONE);
        }
    }else
    {
        edtOther.setVisibility(View.GONE);
    }
    }

    chkSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(chkSelect.isChecked())
            {
                if(islstchild)
                {

                    edtOther.setVisibility(View.VISIBLE);
                    edtOther.requestFocus();

                }
                grouplist.get(groupPos).getIndustry_Based_On_Group().get(childPos).setSelected(true);
                notifyDataSetChanged();

                if(!islstchild)
                {
                    int count=0;
                    for (int i = 0; i < grouplist.get(groupPos).getIndustry_Based_On_Group()
                        .size(); i++) {
                    if (grouplist.get(groupPos).getIndustry_Based_On_Group().get(i).getSelected()==true) {

                        if(i!=grouplist.get(groupPos).getIndustry_Based_On_Group().size()-1)
                        {
                        count = count + 1;
                        }

                        if (count == grouplist.get(groupPos).getIndustry_Based_On_Group().size()-1) {
                            grouplist.get(groupPos).setSelected(true);
                            notifyDataSetChanged();
                        }
                    }

                }   
                }


            }else
            {
                if(!islstchild)
                {
                grouplist.get(groupPos).setSelected(false);
                notifyDataSetChanged();
                }else
                {
                    edtOther.setText("");
                    child.setOtherText("");
                    edtOther.setVisibility(View.GONE);

                }
                grouplist.get(groupPos).getIndustry_Based_On_Group().get(childPos).setSelected(false);
                notifyDataSetChanged();





            }
        }
    });


    edtOther.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            String text= edtOther.getText().toString();
            child.setOtherText(text);
        }

        @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

        }
    });

    return convertView;
}

public IndustryGroup_Level_2 getGroup(int groupPosition) {
    return grouplist.get(groupPosition);
}

public int getGroupCount() {
    return grouplist.size();
}

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

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    final int gpos=groupPosition;

    final IndustryGroup_Level_2 group = (IndustryGroup_Level_2) getGroup(groupPosition);

    if (convertView == null) {
        if (groupPosition==(grouplist.size())-1) {
            convertView = inflater.inflate(R.layout.multilevel_list_item2,
                    null);
        } else {
            convertView = inflater.inflate(R.layout.multilevel_list_item,
                    null);
        }
    }
    if (groupPosition==(grouplist.size())-1) {
        convertView = inflater.inflate(R.layout.multilevel_list_item2,
                null);
    } else {
        convertView = inflater.inflate(R.layout.multilevel_list_item,
                null);
    }

    TextView groupName = (TextView) convertView.findViewById(R.id.textListItem);
    groupName.setText(group.getIndgroupname());
    final CheckBox chkSelect=(CheckBox)convertView.findViewById(R.id.checkBoxInd);
    final EditText edtOther=(EditText)convertView.findViewById(R.id.editTextOtherGroup);

    if (gpos==grouplist.size()-1) {

        if (group.getSelected()) {
            edtOther.setVisibility(View.VISIBLE);
            edtOther.requestFocus();

        } else {
            edtOther.setText("");
            edtOther.setVisibility(View.GONE);

        }

    }else
    {
        edtOther.setText("");
        edtOther.setVisibility(View.GONE);
    }

    if(group.getSelected()!=null)
    {
    chkSelect.setChecked(group.getSelected());
    }



    chkSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("Arraylist size "+grouplist.size());
            System.out.println("Group position "+gpos);

            if(chkSelect.isChecked())
            {
                group.setSelected(true);
                notifyDataSetChanged();


                if(gpos!=grouplist.size()-1)
                {
                for(int i=0;i<group.getIndustry_Based_On_Group().size();i++)
                {
                    if(i!=group.getIndustry_Based_On_Group().size()-1)
                    {
                    group.getIndustry_Based_On_Group().get(i).setSelected(true);
                    notifyDataSetChanged();
                    }
                }
                }
            }else
            {
                group.setSelected(false);
                notifyDataSetChanged();

                if(gpos!=grouplist.size()-1)
                {
                for(int i=0;i<group.getIndustry_Based_On_Group().size();i++)
                {
                    if(i!=group.getIndustry_Based_On_Group().size()-1)
                    {
                    group.getIndustry_Based_On_Group().get(i).setSelected(false);
                    notifyDataSetChanged();
                    }
                }
                }
            }
        }
    });

    if(group.getOtherText()!=null)
    {
        edtOther.setText(group.getOtherText());
    }

    edtOther.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            String text=edtOther.getText().toString();
            group.setOtherText(text);

        }

        @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

        }
    });

    return convertView;
}

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

public boolean hasStableIds() {
    return true;
}

public Industries_Level_3 getChild(int groupPosition, int childPosition) {
    List<Industries_Level_3> ch = grouplist.get(groupPosition)
            .getIndustry_Based_On_Group();
    return ch.get(childPosition);
}
}

【问题讨论】:

    标签: android android-edittext expandablelistview


    【解决方案1】:

    由于您正在回收 convertView(好!),您的 getView() 需要将回收视图中的所有子视图重置为原始状态。

    特别是,您可能需要添加类似

    的内容
    edtOther.setText(null);
    

    就在你初始化edtOtherfindViewById()之后。

    【讨论】:

    • edtOther.setText(null);不起作用....当我使用它时,它的行为就像我没有编写任何代码来将键入的文本保存在edittext中一样! :(
    • 重置为您需要的任何默认值。如果您需要设置一个值(并且已经存储在其他地方),请设置它。
    • 我想我已经在 chkSelect.onclicklistener 上面做了。 edtOther.setText(child.getOtherText().toString());
    • 您在 if 块中有条件地执行此操作。您需要无条件地重置回收视图。
    • 因为我默认设置 json 解析值,如果不使用 if 块,它会给出 nullpointerexception。因为并不是所有的东西都有价值。
    【解决方案2】:

    尝试像这样给convertView 充气:

    if (convertView == null) {
    
        convertView = inflater.inflate(R.layout.multilevel_list_item_child, parent, false);
    }
    

    编辑:

    您的适配器上应该有一个私有类,如下所示:

    private static class GroupHolder {
        TextView groupName;
        CheckBox chkSelect;
        EditText edtOther;
    }
    

    然后,在你的getGroupView 上你应该有这样的东西:

    GroupHolder holder;
    if (convertView == null) {
            holder = new GroupHolder();
            convertView = inflater.inflate(layoutGroup, parent, false);
            /* Initialize these three again like holder.groupName = convertView.findViewById...
            TextView groupName;
            CheckBox chkSelect;
            EditText edtOther;
            */
            convertView.setTag(holder);
    } else {
            holder = (GroupHolder) convertView.getTag();
    }
    
    // and then set fields like this holder.groupName.setText(//TODO);
    

    【讨论】:

    • 无论如何你都应该这样使用它。您的问题可能是很多事情的总和。
    • 您对此有什么解决方案吗? ://
    • 我更改了执行此操作的方法,现在它工作正常。感谢您的回复。 :)
    【解决方案3】:

    很遗憾,我找不到解决这个问题的好方法。所以我改变了我的方法。我没有使用 Edittext,而是使用了 Button 并给了它一个像背景一样的 edittext。在按钮的单击中,出现一个 Alertdialog 并在那里输入值。 And when selecting OK the typed text is set to the button.我认为这是比在列表中使用 edittext 更好的选择。

    我使用此代码为按钮提供了 edittext 背景。

     <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item>
              <shape >
              <solid android:color="#8C8F91" />
              </shape>
          </item>
    
      <!-- main color -->
      <item android:bottom="1dp"
          android:left="1dp"
          android:right="1dp">
          <shape >
              <solid android:color="#ffffff" />
          </shape>
      </item>
    
          <!-- draw another block to cut-off the left and right bars -->
          <item android:bottom="6dp">
              <shape >
                  <solid android:color="#ffffff" />
              </shape>
              </item>
            </layer-list>
    

    按钮点击时显示警报对话框的代码:

    btnOther.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // get prompts.xml view
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.layout_dialog, null);
    
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);
    
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
    
                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);
                    if(child.getOtherText()!=null)
                    {
                        userInput.setText(child.getOtherText().toString());
                    }
                // set dialog message
                alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
    
                    child.setOtherText(userInput.getText().toString());
                    notifyDataSetChanged();
                        }
                      })
                    .setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }
                      });
    
                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();
    
                // show it
                alertDialog.show();
            }
        });
    

    【讨论】:

      【解决方案4】:

      您需要将ExpandableListView layout_height 更改为“match_parent”,并确保始终在getChildView 内部调用getChild(groupPosition, childPosition) 方法以获取数据的新副本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 2017-11-13
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多