I have created small practice android app of contact adding but the app is showing previously added contacts

I have created application to test fragments and adapters. So in one fragment called "AddFrag" I add the contacts and in the other fragment "ViewFrag" it should show the list of added contacts, but instead of showing all the previous contacts , when I click on add it only shows currently added contact only.

AddFrag file code :


    public AddFrag() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.fragment_add,container,false);
        Button add = v.findViewById(R.id.addbtn);
        EditText name = v.findViewById(R.id.name);
        EditText number = v.findViewById(R.id.number);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                String nameString = name.getText().toString();
                String numString = number.getText().toString();
                bundle.putString("name",nameString);
                bundle.putString("number",numString);
                if(numString.isEmpty() || nameString.isEmpty()){
                    
                }
                else {
                    ViewFrag viewFrag = new ViewFrag();
                    viewFrag.setArguments(bundle);
                    getFragmentManager().beginTransaction().replace(R.id.frag,viewFrag).commit();
                }
            }
        });

        return v;
    }
} 

ViewFrag :

public class ViewFrag extends Fragment {

    //code
    ArrayList<String> cName = new ArrayList<String>();
    ArrayList<String> cNumber = new ArrayList<String>() ;
    int img = R.drawable.ic_launcher_background;
    
    public ViewFrag() {
        // Required empty public constructor

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            mParam1 = getArguments().getString(ARG_PARAM1);
//            mParam2 = getArguments().getString(ARG_PARAM2);
//        }
    }

    class MyAdapter extends ArrayAdapter<String>{
        Context context;
        ArrayList<String> rName;
        ArrayList<String> rNumber;
        int rImg;

        MyAdapter(Context c,ArrayList<String> rName,ArrayList<String> rNumber,int rImg){
            super(c,R.layout.row,R.id.nameID,cName);
            this.context = c;
            this.rName = cName;
            this.rNumber = cNumber;
            this.rImg = img;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.row,parent,false);
            ImageView images = row.findViewById(R.id.img);
            TextView rowName = row.findViewById(R.id.nameID);
            TextView rowNumber = row.findViewById(R.id.numberID);
            images.setImageResource(rImg);
            rowName.setText(rName.get(position));
            rowNumber.setText(rNumber.get(position));
            return row;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_view, container, false);

        Bundle bundle = this.getArguments();
        System.out.println(bundle);
        if (bundle != null) {
            cName.add(bundle.getString("name"));
            cNumber.add(bundle.getString("number"));
        }
        else {

        }

        System.out.println("cname:"+cName);

        //code to display the data
        MyAdapter adapter = new MyAdapter(getContext(),cName,cNumber,img);
        ListView listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(adapter);
        return view;
    }
}

MainActivity :

public class MainActivity extends AppCompatActivity {
    Button add,view;
    FragmentTransaction transaction = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        add = (Button)findViewById(R.id.btnAdd);
        view = (Button)findViewById(R.id.btnView);

        AddFrag addfrag = new AddFrag();
        getSupportFragmentManager().beginTransaction().replace(R.id.frag,addfrag).commit();
        //-----------------

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddFrag addFrag = new AddFrag();
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frag,addFrag);
                transaction.commit();
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewFrag viewFrag = new ViewFrag();
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frag,viewFrag);
                transaction.commit();
            }
        });

    }
}

working of app : in main activity there are two buttons : view fragment : it shows ViewFrag add fragment : it shows AddFrag

and AddFrag has button "Add" which also shows ViewFrag after adding contact.

Problem: When I click "Add" it shows currently added contact in ViewFrag and then when i click on View fragment button of main activity it shows empty fragment with no contacts

i want to show every added contacts in ViewFrag without using Database.



Read more here: https://stackoverflow.com/questions/66262268/i-have-created-small-practice-android-app-of-contact-adding-but-the-app-is-showi

Content Attribution

This content was originally published by Nilay Patel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: