-1

I have created this arraylist and with a switch case i am getting the values of the "delhi" arraylist. What i want to do is to create a Listview that will be populated with the items of the arraylist after a button is pressed. I was able to make the items appear on a textview but i cannot do it in a listview

            public void onItemSelected(AdapterView<?> parentView, View 
            selectedItemView, int myPosition, long myID) {
            ArrayList<String> delhi = new ArrayList<String>();

            delhi.add("Virgin Atlantic – London Heathrow");
            delhi.add("British Airways – London Heathrow");

            String country = spinner.getSelectedItem().toString();
            switch (country){
                case "Delhi":
                    record = delhi.get(0);
                    //record1 = delhi.get(1);

                    break;
                case "Hongkong":
                    //do something
                    break;
                // etc,etc,etc
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }


    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Have at look at this article: https://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview – sdraeger May 14 '19 at 21:14

2 Answers2

0

You need to implement the ArrayAdapter for the listview like the way of Using lists in Android wth ListView but I recommend to you to use RecyclerView for achieve the goal that you want Simple Android recycler view example

Nekak Kinich
  • 905
  • 7
  • 10
0

You need an ArrayAdapter which will adapt your ArrayList. Let me give you an example.

public class MainActivity extends Activity {

private ListView mListView;

public void onCreate(Bundle saveInstanceState) {
     setContentView(R.layout.your_layout);

     mListView = (ListView) findViewById(R.id.mListView);

     // Your array list.
        ArrayList<String> delhi = new ArrayList<String>();
        delhi.add("Virgin Atlantic – London Heathrow");
        delhi.add("British Airways – London Heathrow");

     // This is the array adapter, it takes the context of the activity as a 
     // 1st parameter, the type of list view as a 2nd parametr and your 
     // array as a 3rd parameter. android.R.layout.simple_list_item_1 
     // consists of a layout with TextViews.
     ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             delhi );
     // Setting up your Adapter.
     mListView.setAdapter(mArrayAdapter); 
  }
}

Putting this in an OnClickListener will initiate it on a button press.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53