0

I have found some posts but none of the could help me with my problem;

So I have this code:

rowItems = new ArrayList<RowItem>();
    location_names = new String[]{"Schule", "Büro", "Kirche", "Schule"};
    location_longitude = new String[]{"12,23", "56,25", "55,14", "78,15"};
    location_latitude = new String[]{"88,84", "88,79", "98,79", "64,44"};
    switch_status = new String[]{"1", "0", "0", "0"};

    for (int i = 0; i < location_names.length; i++){
        RowItem item = new RowItem(location_names[i], locations_longitude[i], locations_latitude[i], on_off_switch_status[i]);
        rowItems.add(item);
    }

    mylistview = (ListView) findViewById(R.id.list);
    CustomAdapter adapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(adapter);
    mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent settings = new Intent(MainActivity.this, MapsActivity.class);
            startActivity(settings);
        }
    });

The code is then displaying a listView with one item from each String[] in one Row.

This code works great, but I want to add an item to these String[].

I hope you can help me ;)

Phil

Philipp
  • 11
  • 1
  • 5

2 Answers2

0

Look at this post. You must use List<String> to solve it correctly, or in the other hand, create a new String[] with the old String[] lentgh +1 and fill it (not recommended).

You can see an example inside the link I've posted.

Community
  • 1
  • 1
Neonamu
  • 736
  • 1
  • 7
  • 21
0
location_names_array = new ArrayList<>();
    locations_longitude_array = new ArrayList<>();
    locations_latitude_array = new ArrayList<>();

    location_names_array.add("1");
    locations_longitude_array.add("2");
    locations_latitude_array.add("3");




    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            location_names_array.add("1");
            locations_longitude_array.add("2");
            locations_latitude_array.add("3");
            Toast.makeText(getBaseContext(), "Add" , Toast.LENGTH_SHORT    ).show();
        }
    });



    location_names = location_names_array.toArray(new String[location_names_array.size()]);
    locations_longitude = locations_longitude_array.toArray(new String[locations_longitude_array.size()]);
    locations_latitude = locations_latitude_array.toArray(new String[locations_latitude_array.size()]);

    for (int i = 0; i < location_names.length; i++){
        RowItem item = new RowItem(location_names[i], locations_longitude[i], locations_latitude[i]);
        rowItems.add(item);
    }

    mylistview = (ListView) findViewById(R.id.list);
    CustomAdapter adapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(adapter);
    mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent settings = new Intent(MainActivity.this, MapsActivity.class);
            startActivity(settings);
        }
    });

Adding Items to the string-array in the code works, but it doesn't add items in a onClick Method.

Philipp
  • 11
  • 1
  • 5