Mthods does not working

پرسیده شده
فعالیت 1123 روز پیش
دیده شده 407 بار
0

سلام پوریا جان من یه آداپتر دارم که اینه 

 

package doit.mrkhoshneshin.ir;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class ContactNewAdapter extends RecyclerView.Adapter <ContactNewAdapter.ContactViewHolder> {

    List<Contact> contacts = new ArrayList<>();

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_contact_layout , parent , false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        holder.bindContact(contacts.get(position));
    }

    public void addContact(Contact contact){
        contacts.add(0,contact);
        notifyItemInserted(0);
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }


    public class ContactViewHolder extends RecyclerView.ViewHolder{

        TextView fullName;
        TextView age;
        TextView gender;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            fullName = itemView.findViewById(R.id.tv_recycler_fullName);
            age = itemView.findViewById(R.id.tv_recycler_age);
            gender = itemView.findViewById(R.id.tv_recycler_gender);
        }
        public void bindContact(Contact contact){
            fullName.setText(contact.getFullName());
            age.setText(contact.getAge());
            if(contact.getGender() == 'M')
                gender.setText("آقا");
            if(contact.getGender() == 'F')
                gender.setText("خانم");
        }
    }

}

در حالتی که من بخوام فقط تعدادی آیتم رو نشون بدم و متد گِت آیتم کاونت یه مقدار صحیح برگردونه ، این آداپتر کار میکنه و مشکلی نداره

 

اما وقتی کدی که بالا گذاشتم رو اجرا میکنم متد ها کال نمیشن اصلا

و فقط متد اد کانتکت کال میشه که اونم چون از اکتیویتی اصلی کال شده و تمام میشه اجرا

 

کانتکتی که میفرستم از دیالوگ میاد که کدش رو اینجا میذارم

اینم دیالوگ 

 

package doit.mrkhoshneshin.ir;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

public class AddContactDialog extends DialogFragment {

     ContactNewAdapter contactNewAdapter;
     public char gender;
     AddNewContact onSendContact;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        onSendContact = (AddNewContact) context;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        if(contactNewAdapter == null)
            contactNewAdapter = new ContactNewAdapter();

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.add_contact_dialog , null , false);

        TextInputEditText fullNameEt = view.findViewById(R.id.et_dialog_fullName);
        TextInputLayout fullNameEtl = view.findViewById(R.id.etl_dialog_fullName);

        TextInputEditText ageEt = view.findViewById(R.id.et_dialog_age);
        TextInputLayout ageEtl = view.findViewById(R.id.etl_dialog_age);

        RadioGroup radioGroup = view.findViewById(R.id.radiogp_dialog_gender);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.radio_btn_woman)
                    gender = 'F';
                else
                    gender = 'M';
            }
        });
        Button btnSave = view.findViewById(R.id.btn_dialog_save);

        fullNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(fullNameEt.length() == 0){
                    fullNameEtl.setError("نام و نام خانوادگی نمی تواند خالی باشد");
                } else
                    fullNameEtl.setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ageEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(ageEt.length() ==0){
                    ageEtl.setError("سن نمی تواند خالی باشد");
                }else
                    ageEtl.setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fullNameEt.length() > 0){
                    if(ageEt.length() > 0){

                        Contact contact = new Contact();
                        contact.setFullName(fullNameEt.getText().toString());
                        contact.setAge(Integer.parseInt(ageEt.getText().toString()));
                        contact.setGender(gender);
//                        contactAdapter.addContact(contact);
                        onSendContact.sendContact(contact);
                        dismiss();
                    }

                }
                if(fullNameEt.length() == 0)
                    fullNameEtl.setError("نام و نام خانوادگی نمی تواند خالی باشد");
                if(ageEt.length() ==0)
                    ageEtl.setError("سن نمی تواند خالی باشد");
            }
        });



        builder.setView(view);
        return builder.create();
    }

    public interface AddNewContact {
        void sendContact(Contact contact);
    }

}

 

فایل پیوست

mohammadjavad khoshneshin
mohammadjavad khoshneshin

12 فروردین 00

0
حذف شده

سلام وقت بخیر محمد جواد جان 

 

پروژه رو ممنون میشم اپلود کنین تا براتون دیباگ کنم .

برای اپلود میتونین از پیکوفایل استفاده کنین یا گوگل درایو.

فایل پیوست

پوریا شفیعی

توسط

پوریا شفیعی

12 فروردین 00