복붙노트

[ANDROID] 반복 형태의 동적 형태

ANDROID

반복 형태의 동적 형태

해결법


  1. 1.AND : 당신은 recyclerview으로 가야한다

    AND : 당신은 recyclerview으로 가야한다

    recyclerview 하나의 항목에 대한 그 같은 레이아웃 만들기

    당신은 RecyclerView.Adapter 클래스의 값을 얻을 수 있습니다

    여기 SAMPLE 코드는

    public class AddmoreActivity extends AppCompatActivity {
    
        RecyclerView myRc;
        ArrayList<AddMorePojo> arrayList = new ArrayList<>();
        Button btnGetData;
        AddMoreAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_addmore);
    
            myRc = (RecyclerView) findViewById(R.id.myRc);
            btnGetData = (Button) findViewById(R.id.btnGetData);
    
            myRc.setHasFixedSize(true);
            myRc.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    
            AddMorePojo addMorePojo = new AddMorePojo();
            addMorePojo.setAddress("");
            addMorePojo.setUserName("");
            arrayList.add(addMorePojo);
    
    
            adapter = new AddMoreAdapter(this, arrayList);
            myRc.setAdapter(adapter);
    
            btnGetData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<AddMorePojo> pojoArrayList = adapter.getArrayList();
    
                    for (int i = 0; i < pojoArrayList.size(); i++) {
    
                        Log.e("Name " + i, pojoArrayList.get(i).getUserName() + "");
                        Log.e("Pass " + i, pojoArrayList.get(i).getPass() + "");
                        Log.e("PHONE " + i, pojoArrayList.get(i).getPhone() + "");
                        Log.e("Address " + i, pojoArrayList.get(i).getAddress() + "");
                    }
                }
            });
    
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.user33.workingtestapp.AddmoreActivity">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
        <Button
            android:id="@+id/btnGetData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="get Data" />
    </RelativeLayout>
    
    public class AddMorePojo {
    
        String userName, phone, pass, Address;
    
        public AddMorePojo() {
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getPass() {
            return pass;
        }
    
        public void setPass(String pass) {
            this.pass = pass;
        }
    
        public String getAddress() {
            return Address;
        }
    
        public void setAddress(String address) {
            Address = address;
        }
    }
    
    public class AddMoreAdapter extends RecyclerView.Adapter<AddMoreAdapter.ViewHolder> {
        Context context;
        ArrayList<AddMorePojo> arrayList;
    
        public AddMoreAdapter(Context context, ArrayList<AddMorePojo> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @Override
        public AddMoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            View view = LayoutInflater.from(context).inflate(R.layout.addmorelayout, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(AddMoreAdapter.ViewHolder holder, int position) {
    
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public ArrayList<AddMorePojo> getArrayList()
        {
            return arrayList;
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            EditText edtName, edtPhone, edtPass, edtAdrress;
    
            public ViewHolder(View itemView) {
                super(itemView);
    
                edtName = itemView.findViewById(R.id.edtUname);
                edtPhone = itemView.findViewById(R.id.edtPhone);
                edtPass = itemView.findViewById(R.id.edtPass);
                edtAdrress = itemView.findViewById(R.id.edtAddress);
    
                edtName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                        addMorePOJO.setUserName(charSequence + "");
                        arrayList.set(getAdapterPosition(), addMorePOJO);
    
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
    
                    }
                });
    
                edtPhone.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                        addMorePOJO.setPhone(charSequence + "");
                        arrayList.set(getAdapterPosition(), addMorePOJO);
    
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
    
                    }
                });
                edtPass.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                        addMorePOJO.setPass(charSequence + "");
                        arrayList.set(getAdapterPosition(), addMorePOJO);
    
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
    
                    }
                });
    
                edtAdrress.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                        AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                        addMorePOJO.setAddress(charSequence + "");
                        arrayList.set(getAdapterPosition(), addMorePOJO);
    
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
    
                    }
                });
    
    
            }
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="10dp"
        app:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/edtUname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter User Name" />
    
            <EditText
                android:id="@+id/edtPass"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="********"
                android:imeOptions="actionNext"
                android:inputType="textPassword" />
    
    
            <EditText
                android:id="@+id/edtPhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter Phone Number"
                android:imeOptions="actionNext"
                android:inputType="numberDecimal" />
    
            <EditText
                android:id="@+id/edtAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter Address"
                android:imeOptions="actionNext"
                android:inputType="text" />
    
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    
  2. from https://stackoverflow.com/questions/47975286/dynamic-form-with-repeating-form by cc-by-sa and MIT license