Vishal Chepuri
Vishal Chepuri

Reputation: 320

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

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);
            }
        });
    }

Upvotes: 0

Views: 40

Answers (1)

ufoq
ufoq

Reputation: 103

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.

Upvotes: 2

Related Questions