When adding Values to Arraylist from one class to another, Listview does not display updated values Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of When adding Values to Arraylist from one class to another, Listview does not display updated values without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am having one Array list in Class-A and two textfields one from Class-A and other from Class-B,
While adding values to Array List through Class-A textfield it works fine for me,

and when trying to add from Class-B textfield, it gets added to Array list but does not display the updated list in the listview(Blank)
Below is the code i have tried till now

Class-A :-

public static ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;
    ListView listView;
    EditText quickTask;
    Button addButton;
    Button moreButton;

    public static ArrayList<String> addToList( String i ) {
        arrayList.add(i);
        return arrayList;
    }

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

        quickTask = (EditText) findViewById(R.id.quicktaskeditText);
        listView = (ListView) findViewById(R.id.TasksListView);
        arrayAdapter = new ArrayAdapter<String>(ClassA.this, android.R.layout.simple_list_item_1,arrayList);

        addButton = (Button) findViewById(R.id.Addbutton);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String task = quickTask.getText().toString();
                arrayList.add(task);
                listView.setAdapter(arrayAdapter);
                arrayAdapter.notifyDataSetChanged();
            }
        });
    moreButton = (Button) findViewById(R.id.moreButton);

    moreButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent secondActIntent = new Intent(getApplicationContext(),ClassB.class);
            startActivity(secondActIntent);
        }
    });
}

Class-B :-

    EditText Title;
    Button AddButton;
    ArrayList<String> alist;

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

        Title = (EditText) findViewById(R.id.Title);
        AddButton = (Button) findViewById(R.id.secondAddButton);

        AddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String task = Title.getText().toString();
                //Adding text to Class-A arraylist
                alist = ClassA.addToList(task);
                Intent startIntent = new Intent(getApplicationContext(),ClassA.class);
               startActivity(startIntent);
            }
        });
    }

Answer

Because you don’t notify ListView that contents of ArrayList changed.

You have notifyDataSetChanged only in button listener. You need to add it in Class-A#onResume or in addTolist.

We are here to answer your question about When adding Values to Arraylist from one class to another, Listview does not display updated values - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji