Android 排序arraylist时索引自动边界
我正在将Actionbarsherlock添加到我的应用程序中,以更新Android 2.2/2.3用户的UI。ABS工作得很好,但我发现旧设备存在一个问题,在重新启动应用程序后打开应用程序后,ListView会挂起。该应用程序列出所有具有互联网权限的应用程序,然后添加一些特殊应用程序(该应用程序是防火墙),并在尝试显示信息时挂起 最初,代码在使用标准数组构建列表后缓存应用程序。我想把应用程序从缓存中移开,因为我认为这是挂起的一个重要原因。因此,我一直在将所有内容从数组移动到ArrayList,以便于使用。我遇到了一个IndexOutofBounds,但更正了那个,但这一个完全难住了我。这是我获取应用程序和排序代码的代码 任何帮助都将不胜感激,如果需要任何其他代码,请询问! 提前谢谢 应用程序列表代码:Android 排序arraylist时索引自动边界,android,arraylist,Android,Arraylist,我正在将Actionbarsherlock添加到我的应用程序中,以更新Android 2.2/2.3用户的UI。ABS工作得很好,但我发现旧设备存在一个问题,在重新启动应用程序后打开应用程序后,ListView会挂起。该应用程序列出所有具有互联网权限的应用程序,然后添加一些特殊应用程序(该应用程序是防火墙),并在尝试显示信息时挂起 最初,代码在使用标准数组构建列表后缓存应用程序。我想把应用程序从缓存中移开,因为我认为这是挂起的一个重要原因。因此,我一直在将所有内容从数组移动到ArrayList,
int count = 0;
try {
final PackageManager pkgmanager = ctx.getPackageManager();
final List<ApplicationInfo> installed = pkgmanager
.getInstalledApplications(PackageManager.GET_META_DATA);
final HashMap<Integer, DroidApp> map = new HashMap<Integer, DroidApp>();
final Editor edit = prefs.edit();
boolean changed = false;
String name = null;
String cachekey = null;
final String cacheLabel = "cache.label.";
DroidApp app = null;
for (final ApplicationInfo apinfo : installed) {
count = count + 1;
if(applist != null){
applist.doProgress(count);
}
boolean firstseen = false;
app = map.get(apinfo.uid);
// filter applications which are not allowed to access the
// Internet
if (app == null
&& PackageManager.PERMISSION_GRANTED != pkgmanager
.checkPermission(Manifest.permission.INTERNET,
apinfo.packageName)) {
continue;
}
// try to get the application label from our cache -
// getApplicationLabel() is horribly slow!!!!
cachekey = cacheLabel + apinfo.packageName;
name = prefs.getString(cachekey, "");
if (name.length() == 0) {
// get label and put on cache
name = pkgmanager.getApplicationLabel(apinfo).toString();
edit.putString(cachekey, name);
changed = true;
firstseen = true;
}
if (app == null) {
app = new DroidApp();
app.uid = apinfo.uid;
app.names = new ArrayList<String>();
app.names.add(name);
app.appinfo = apinfo;
map.put(apinfo.uid, app);
} else {
app.names.add(name);
}
app.firstseen = firstseen;
// check if this application is selected
if (!app.selected_wifi
&& Arrays.binarySearch(selected_wifi, app.uid) >= 0) {
app.selected_wifi = true;
}
if (!app.selected_3g
&& Arrays.binarySearch(selected_3g, app.uid) >= 0) {
app.selected_3g = true;
}
if (!app.selected_roaming
&& Arrays.binarySearch(selected_roaming, app.uid) >= 0) {
app.selected_roaming = true;
}
if (!app.selected_vpn
&& Arrays.binarySearch(selected_vpn, app.uid) >= 0) {
app.selected_vpn = true;
}
}
if (changed) {
edit.commit();
}
/* add special applications to the list */
List<DroidApp> special = new ArrayList<DroidApp>();
special.add(new DroidApp(
SPECIAL_UID_ANY,
"(Any application) - Same as selecting all applications", false, false, false, false));
special.add(new DroidApp(SPECIAL_UID_KERNEL, "(Kernel) - Linux kernel", false, false, false, false));
special.add(new DroidApp(android.os.Process.getUidForName("root"), "(root) - Applications running as root", false, false, false, false));
special.add(new DroidApp(android.os.Process.getUidForName("media"),"Media server", false, false, false, false));
special.add(new DroidApp(android.os.Process.getUidForName("vpn"), "VPN networking", false, false, false, false));
special.add(new DroidApp(android.os.Process.getUidForName("shell"), "Linux shell", false, false, false, false));
special.add(new DroidApp(android.os.Process.getUidForName("gps"), "GPS", false, false, false, false));
for (int i = 0; i < special.size(); i++) {
app = special.get(i);
if (app.uid != -1 && !map.containsKey(app.uid)) {
// check if this application is allowed
if (Arrays.binarySearch(selected_wifi, app.uid) >= 0) {
app.selected_wifi = true;
}
if (Arrays.binarySearch(selected_3g, app.uid) >= 0) {
app.selected_3g = true;
}
if (Arrays.binarySearch(selected_roaming, app.uid) >= 0) {
app.selected_roaming = true;
}
if (Arrays.binarySearch(selected_vpn, app.uid) >= 0) {
app.selected_vpn = true;
}
map.put(app.uid, app);
}
}
/* convert the map into an array */
applications = new ArrayList<DroidApp>(map.values());
return applications;
int count=0;
试一试{
最终PackageManager pkgmanager=ctx.getPackageManager();
最终安装列表=pkgmanager
.GetInstalledApplication(PackageManager.GET_元数据);
final HashMap map=新HashMap();
最终编辑器编辑=prefs.edit();
布尔值=假;
字符串名称=null;
字符串cachekey=null;
最后一个字符串cacheLabel=“cache.label。”;
DroidApp=null;
用于(最终应用程序信息apinfo:已安装){
计数=计数+1;
if(applist!=null){
应用程序进程(计数);
}
布尔值firstseen=false;
app=map.get(apinfo.uid);
//筛选不允许访问的应用程序
//互联网
如果(app==null
&&PackageManager.PERMISSION_已授予!=pkgmanager
.checkPermission(Manifest.permission.INTERNET、,
apinfo.packageName){
继续;
}
//尝试从缓存中获取应用程序标签-
//getApplicationLabel()非常慢!!!!
cachekey=cacheLabel+apinfo.packageName;
name=prefs.getString(cachekey,“”);
if(name.length()==0){
//获取标签并放入缓存
name=pkgmanager.getApplicationLabel(apinfo.toString();
edit.putString(cachekey,name);
更改=正确;
第一次看到=正确;
}
如果(app==null){
app=新的DroidApp();
app.uid=apinfo.uid;
app.names=new ArrayList();
app.names.add(名称);
app.appinfo=apinfo;
map.put(apinfo.uid,app);
}否则{
app.names.add(名称);
}
app.firstseen=firstseen;
//检查是否选择了此应用程序
如果(!app.selected_wifi
&&Arrays.binarySearch(选定的\u wifi,app.uid)>=0){
app.selected_wifi=true;
}
如果(!app.selected_3g
&&Arrays.binarySearch(选中的\u 3g,app.uid)>=0){
app.selected_3g=真;
}
如果(!app.selected\u漫游
&&Arrays.binarySearch(选定的\u漫游,app.uid)>=0){
app.selected_roaming=true;
}
如果(!app.selected_vpn
&&Arrays.binarySearch(选定的\u vpn,app.uid)>=0){
app.selected_vpn=true;
}
}
如果(更改){
edit.commit();
}
/*将特殊应用程序添加到列表中*/
List special=new ArrayList();
特殊。添加(新DroidApp)(
特别的(如有),,
“(任何申请)-与选择所有申请相同”,假、假、假、假);
add(新的DroidApp(特殊的UID内核,“(内核)-Linux内核”,false,false,false);
添加(新的DroidApp(android.os.Process.getUidForName(“root”),“(root)-以root身份运行的应用程序”,false,false,false,false));
添加(新的DroidApp(android.os.Process.getUidForName(“媒体”),“媒体服务器”,false,false,false,false));
添加(新的DroidApp(android.os.Process.getUidForName(“vpn”),“vpn网络”,false,false,false));
添加(新的DroidApp(android.os.Process.getUidForName(“shell”),“linuxshell”,false,false,false));
添加(新的DroidApp(android.os.Process.getUidForName(“gps”),“gps”,false,false,false,false));
对于(int i=0;i=0){
app.selected_wifi=true;
}
if(Arrays.binarySearch(selected_3g,app.uid)>=0){
app.selected_3g=真;
}
if(Arrays.binarySearch(selected\u roaming,app.uid)>=0){
app.selected_roaming=true;
}
if(Arrays.binarySearch(selected\u vpn,app.uid)>=0){
app.selected_vpn=true;
}
map.put(app.uid,app);
}
}
/*将地图转换为数组*/
applications=newarraylist(map.values());
退货申请;
排序代码:
class ApplicationSort implements Comparator<DroidApp> {
@Override
public int compare(DroidApp o1, DroidApp o2) {
if (o1.firstseen != o2.firstseen) {
return (o1.firstseen ? -1 : 1);
}
boolean o1_selected;
boolean o2_selected;
boolean vpnenabled = getApplicationContext()
.getSharedPreferences(Api.PREFS_NAME, 0).getBoolean(
Api.PREF_VPNENABLED, false);
boolean roamenabled = getApplicationContext()
.getSharedPreferences(Api.PREFS_NAME, 0).getBoolean(
Api.PREF_ROAMENABLED, false);
if (vpnenabled && !roamenabled) {
o1_selected = o1.selected_3g || o1.selected_wifi
|| o1.selected_vpn;
o2_selected = o2.selected_3g || o2.selected_wifi
|| o2.selected_vpn;
if (o1_selected == o2_selected) {
return String.CASE_INSENSITIVE_ORDER.compare(
o1.names.get(0).toString(), o2.names.get(0).toString());
}
if (o1_selected)
return -1;
}
if (roamenabled && !vpnenabled) {
o1_selected = o1.selected_3g || o1.selected_wifi
|| o1.selected_roaming;
o2_selected = o2.selected_3g || o2.selected_wifi
|| o2.selected_roaming;
if (o1_selected == o2_selected) {
return String.CASE_INSENSITIVE_ORDER.compare(
o1.names.get(0).toString(), o2.names.get(0).toString());
}
if (o1_selected)
return -1;
}
if (roamenabled && vpnenabled) {
o1_selected = o1.selected_3g || o1.selected_wifi
|| o1.selected_roaming || o1.selected_vpn;
o2_selected = o2.selected_3g || o2.selected_wifi
|| o2.selected_roaming || o2.selected_vpn;
if (o1_selected == o2_selected) {
return String.CASE_INSENSITIVE_ORDER.compare(
o1.names.get(0).toString(), o2.names.get(0).toString());
}
if (o1_selected)
return -1;
}
if (!roamenabled && !vpnenabled) {
o1_selected = o1.selected_3g || o1.selected_wifi;
o2_selected = o2.selected_3g || o2.selected_wifi;
if (o1_selected == o2_selected) {
return String.CASE_INSENSITIVE_ORDER.compare(
o1.names.get(0).toString(), o2.names.get(0).toString());
}
if (o1_selected)
return -1;
}
return 1;
}
}
类应用程序端口实现Comparator{
@凌驾
公共整数比较(DroidApp o1,DroidApp o2){
if(o1.FirstSeed!=o2.FirstSeed){
返回(o1.1)第一次看到
private void createListView(final String searching) {
this.dirty = false;
boolean results = false;
List<DroidApp> namesearch = new ArrayList<DroidApp>();
final List<DroidApp> appnames = Api.getApps(this, null);
if (searching != null && searching.length() > 1) {
for (DroidApp app : appnames) {
for (String str : app.names) {
if (str.contains(searching.toLowerCase())
|| str.toLowerCase().contains(
searching.toLowerCase())) {
namesearch.add(app);
results = true;
}
}
}
}
final List<DroidApp> apps = results ? namesearch
: searching.equals("") ? appnames
: new ArrayList<Api.DroidApp>();
// Sort applications - selected first, then alphabetically
Collections.sort(apps, new ApplicationSort());
o1.names.get(0) and o2.names.get(0)