安卓分割空字符串数组长度为1

空值分割总有1个长度,困惑了很久。

Java split分割空字符串得到的数组长度为1_java数组length为1的情况-CSDN博客

计数

java 复制代码
int count_bookmark = 0;
SharedPreferences SP;

SP = PreferenceManager.getDefaultSharedPreferences(this);
String s = SP.getString("bookmarks", "");        
if (!s.isEmpty()) {
    String[] sa = s.split(":");
    count_bookmark = sa.length;
}

加载

java 复制代码
void loadBookmarks() {
    String s = sharedPreferences.getString("bookmarks", "");    
    String[] sa = s.split(":");
    Collections.addAll(list, sa);
    list.remove("");
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);
}

添加

java 复制代码
SharedPreferences SP;
SharedPreferences.Editor SPE;

String s = SP.getString("bookmarks", "");
if (s.isEmpty())
    SPE.putString("bookmarks", path);
else
    SPE.putString("bookmarks", ":" + path);
SPE.apply();
相关推荐
码农coding1 天前
android 12 中的VSYNC的请求
android
阿pin1 天前
Android随笔-Activity
android·activity
YF02111 天前
详解Android所有文件访问权限
android
时间的拾荒人1 天前
MySQL C语言连接 - 从入门到实战
android·c语言·mysql
Meteors.1 天前
Android性能优化:01. 指标体系 + 分析工具链
android
jike_20261 天前
安卓平台免费录音转文字工具深度测评:5 款 APP 功能对比与选型指南
android·智能电视
Code Man1 天前
Windows 下使用 Appium
android·windows·appium
码农coding1 天前
android 12 中的VSYNC接收
android
GitLqr1 天前
Flutter 3.44 性能飞跃:深度解析 Android Platform View 的 HCPP 新特性
android·flutter·性能优化
码农coding1 天前
android 12 SurfaceFlinger中的CompositionEngine
android