0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i have added both listView.setAdapter(arrayAdapter); and arrayAdapter.notifyDataSetChanged(); inside addToList, but i see the same issue
@Vishal Manipulating views of non-presented View may be problematic anyway (bugs / wasted cpu cycles). So it's better to have it in onResume.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.