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

人走茶凉 提交于 2019-11-30 08:58:50

you can achieve this using the custom list view.

find the example of listview with edittext is here

Vikas

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_All_Class_Model> get_class_details;
    LayoutInflater inflater;
    Context context;
    private int x = 1;

    public Get_Class_Adapter(Context context,
            ArrayList<Get_All_Class_Model> 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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!