【问题标题】:Add String to StringArray in ListView将字符串添加到 ListView 中的字符串数组
【发布时间】:2016-02-11 16:03:05
【问题描述】:

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

所以我有这个代码:

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[] 中添加一个项目。

希望你能帮助我;)

菲尔

【问题讨论】:

  • 什么是行项?并发布您的适配器
  • 我不明白你想要什么?此代码假设显示 4 个项目。要显示 5 吗?也许您无法添加评论,所以请编辑您的问题。

标签: java android arrays string


【解决方案1】:

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

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

【讨论】:

  • 在程序中添加项目有效,但在 onClick 方法中没有任何作用
【解决方案2】:
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 方法中添加项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 2012-12-15
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多