在ListView中将String添加到StringArray

时间:2016-02-11 16:03:05

标签: java android arrays string

我找到了一些帖子,但没有一个可以帮助我解决我的问题;

所以我有这段代码:

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

然后代码显示一个listView,其中一行中的每个String []都有一个项目。

这段代码效果很好,但我想在这些String []中添加一个项目。

我希望你能帮助我;)

菲尔

2 个答案:

答案 0 :(得分:0)

看看this post。您必须使用List<String>来正确解决它,或者另一方面,使用旧的String [] lentgh +1创建一个新的String []并填充它(不推荐)。

您可以在我发布的链接中看到一个示例。

答案 1 :(得分: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);
        }
    });

在代码中将字符串数组添加到项目中,但它不会在onClick方法中添加项目。