1

I want to add elements in simple_list_item_2. but i don't know how to add. I created 2 ArrayLists. Here is my code

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listv=(ListView) findViewById(R.id.listview);

    arraylist1=new ArrayList<String>();
    arraylist1.add("a");
    arraylist1.add("b");
    arraylist1.add("c");

    arraylist2=new ArrayList<String>();
    arraylist2.add("1");
    arraylist2.add("2");
    arraylist2.add("3");

    adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_2,arraylist1);
    listv.setAdapter(adapter);
}

ArrayList<String> arraylist1,arraylist2;
ArrayAdapter adapter;
ListView listv;
}
Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
Siddharth Gharge
  • 112
  • 3
  • 13

1 Answers1

0

Just as you added the elements before you can add them anytime you want as,

arraylist1.add("a");

And then you just have notify the adapter that the data had been changed, to do that put the below line of code after you add or remove data from the array list...

adapter.notifyDatasetChanged();
Rohit Jagtap
  • 1,660
  • 1
  • 14
  • 12