How to add EditText in listview and get its value dynamically in all the rows?

前端 未结 3 1060
暖寄归人
暖寄归人 2020-12-31 12:09

I have Checkbox and EditText and a Textview in a listView. It gets value for the text view from a list. Checkbox will be checked dynamically. In the same way EditText also c

3条回答
  •  太阳男子
    2020-12-31 12:50

    Just keep viewHolder.address.setTag(position) and it works perfect cheers.

    Adapter Class:

    package com.qzick.adapter;
    
    import java.util.ArrayList;
    import android.content.Context;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.example.qzick.R;
    import com.qzick.model.Get_All_Class_Model;
    
    public class Get_Class_Adapter extends BaseAdapter {
        protected ArrayList get_class_details;
        LayoutInflater inflater;
        Context context;
        private int x = 1;
    
        public Get_Class_Adapter(Context context,
                ArrayList get_class_details) {
            this.get_class_details = get_class_details;
            this.inflater = LayoutInflater.from(context);
            this.context = context;
    
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return get_class_details.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return get_class_details.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = this.inflater.inflate(
                        R.layout.activity_adapter_class_ll, parent, false);
    
                holder.textclass = (TextView) convertView
                        .findViewById(R.id.text_class_ll);
    
                holder.txtid = (TextView) convertView.findViewById(R.id.text_id_ll);
    
                holder.checkclass = (CheckBox) convertView
                        .findViewById(R.id.check_class_LL);
    
                holder.edtsection = (EditText) convertView
                        .findViewById(R.id.edttxt_addsection_ll);
    
                holder.checkclass
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView,
                                    boolean isChecked) {
    
                                int getPosition = (Integer) buttonView.getTag();
    
                                get_class_details.get(getPosition).setChecked(
                                        buttonView.isChecked());
    
                                notifyDataSetChanged();
                            }
                        });
    
                convertView.setTag(holder);
                convertView.setTag(R.id.check_class_LL, holder.checkclass);
                convertView.setTag(R.id.edttxt_addsection_ll, holder.edtsection);
    
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            holder.checkclass.setTag(position);
            holder.edtsection.setTag(position);
    
            holder.edtsection.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    int pos = (Integer) holder.edtsection.getTag();
    
                    get_class_details.get(pos).setEdtsections(s.toString());
    
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    
            holder.txtid.setText(get_class_details.get(position).getId());
            holder.textclass.setText(get_class_details.get(position).getText());
            holder.edtsection.setText(get_class_details.get(position)
                    .getEdtsections());
    
            holder.textclass.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    x++;
    
                    if (x % 2 == 0) {
                        holder.checkclass.setChecked(false);
    
                    } else {
                        holder.checkclass.setChecked(true);
                    }
    
                }
            });
    
            holder.checkclass.setChecked(get_class_details.get(position)
                    .isChecked());
            return convertView;
        }
    
        private class ViewHolder {
            TextView textclass, txtid;`enter code here`
            CheckBox checkclass;
            EditText edtsection;
        }
    }
    

提交回复
热议问题