You’ve two arrayList one local and another one global.
onClick()
method you’re adding your item in global arrayList having reference name fruits but your ListView using local arrayList also having same name fruits, so don’t create local list inside onCreate()
method. Also after adding new fruit call notifyDataSetChanged()
on your adapter.
ArrayList<String> fruits;
ListAdapter myAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fruits = new ArrayList<String>();
fruits.add("apple");
fruits.add("banana");
fruits.add("mango");
fruits.add("orange");
fruits.add("peach");
myAdapter = new CustomAdapter(this,fruits);
....
}
public void onClick (View view) {
final EditText input = (EditText) findViewById(R.id.editText);
String name = input.getText().toString();
fruits.add(name);
myAdapter.notifyDataSetChanged();
}
0
solved Updating ArrayList with onlick not working