简体   繁体   中英

Android Studio: ExpandableListView and EditText

I have an ExpandableListView with an EditText in it. It works well, but when I put a number in one row, the number is visible in another row as well. I found already some questions regarding this problem (with an holder as solution) but I am not able to use it in my case. Can anybody help me?

Here is the code:

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
    View view = convertView;
    if(view == null){
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.child_lm, parent, false);
    }
    final String childName = (String)getChild(groupPosition, childPosition);
    final TextView childItem = (TextView)view.findViewById(R.id.child_layout);
    childItem.setText(childName);


        einheit1 = data2[childPosition];
        einheit2 = data3[childPosition];
        einheit3 = data4[childPosition];
        einheit4 = data5[childPosition];
        mengenbeschreibung = data6[childPosition];


    final EditText mz= (EditText) view.findViewById(R.id.etMz);
    final String menge=mz.getText().toString();

    Drawable drawable = mz.getBackground(); 
    drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP); 
    if(Build.VERSION.SDK_INT > 16) {
        mz.setBackground(drawable); 
    }else{
        mz.setBackgroundDrawable(drawable);  
    }

    TextView eh1=(TextView)view.findViewById(R.id.tv1);
    eh1.setText(einheit1);
    TextView eh2=(TextView)view.findViewById(R.id.tv2);
    eh2.setText(einheit2);
    TextView eh3=(TextView)view.findViewById(R.id.tv3);
    eh3.setText(einheit3);
    TextView eh4=(TextView)view.findViewById(R.id.tv4);
    eh4.setText(einheit4);
    TextView mengenangabe=(TextView)view.findViewById(R.id.tvMengenbeschreibung);
    mengenangabe.setText(mengenbeschreibung);

    final View finalView1 = view;
    mz.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(
                Editable arg0) {
        }
        public void beforeTextChanged(
                CharSequence s, int start,
                int count, int after) {
        }
        public void onTextChanged(
                CharSequence s, int start,
                int before, int count) {
            TextView eh1=(TextView) finalView1.findViewById(R.id.tv1);
            TextView eh2=(TextView)finalView1.findViewById(R.id.tv2);
            TextView eh3=(TextView)finalView1.findViewById(R.id.tv3);
            TextView eh4=(TextView)finalView1.findViewById(R.id.tv4); if(mz.getText().toString().equals(null)||mz.getText().toString().equals("")) {
                }else{
                    double test10= (Math.round(Float.parseFloat(mz.getText().toString().replace(",","."))*Float.parseFloat(data2[childPosition])/100*2));
                    float test1 = (float) (test10/2);
                    eh1.setText(String.valueOf(test1));

                    double test20= (Math.round(Float.parseFloat(mz.getText().toString().replace(",","."))*Float.parseFloat(data3[childPosition])/100*2));
                    float test2 = (float) (test20/2);
                    eh2.setText(String.valueOf(test2));

                    double test30= (Math.round(Float.parseFloat(mz.getText().toString().replace(",","."))*Float.parseFloat(data4[childPosition])/100*2));
                    float test3 = (float) (test30/2);
                    eh3.setText(String.valueOf(test3));

                    double test40= (Math.round(Float.parseFloat(mz.getText().toString().replace(",","."))*Float.parseFloat(data5[childPosition])/100*2));
                    float test4 = (float) (test40/2);
                    eh4.setText(String.valueOf(test4));
                }
    });

    return view;
}

mz.addTextChangedListener should be in your MainActivity OnCreate(). At least that's what I guess, did not check your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM