0

这是我从共享偏好中获得的,

ApplicationInfo{1622e954 com.example.user.example},ApplicationInfo{1aed20fd com.example.browhistlog}

知道十六进制指的是什么吗?是否可以根据我检索到的信息将列表过滤到 2 应用程序中?如果可能的话,我能做些什么来实现这一点(任何参考)?

在此处输入图像描述
;

ListView myList;
Button getChoice, clearAll, selectAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myList = (ListView)findViewById(android.R.id.list);
    getChoice = (Button)findViewById(R.id.getchoice);
    clearAll = (Button)findViewById(R.id.clearall);
    selectAll = (Button)findViewById(R.id.selectall);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(adapter);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if(sharedpreferences.contains(MyPREFERENCES)){
        LoadSelections();
    }

    getChoice.setOnClickListener(new Button.OnClickListener() {


        @Override
        public void onClick(View v) {

            String selected = "";
            int cntChoice = myList.getCount();

            SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
            for (int i = 0; i < cntChoice; i++) {
                if (sparseBooleanArray.get(i)) {
                    selected += myList.getItemAtPosition(i).toString() + "\n";
                    System.out.println("Checking list while adding:" + myList.getItemAtPosition(i).toString());
                    SaveSelections();
                }

            }

            Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();

        }
    });

    clearAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ClearSelections();
        }
    });

    selectAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SelectAllSelections();
        }
    });

    packageManager = getPackageManager();

    new LoadApplications().execute();
   }
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user

    SharedPreferences.Editor prefEditor = sharedpreferences.edit();
    String savedItems = getSavedItems();
    prefEditor.putString(MyPREFERENCES.toString(), savedItems);
    prefEditor.commit();
}

private String getSavedItems() {
    String savedItems = "";
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.myList.isItemChecked(i)) {
            if (savedItems.length() > 0) {
                savedItems += "," + this.myList.getItemAtPosition(i);
            } else {
                savedItems += this.myList.getItemAtPosition(i);
            }
        }
    }
    return savedItems;
}

private void LoadSelections() {
// if the selections were previously saved load them

    if (sharedpreferences.contains(MyPREFERENCES.toString())) {

        String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
        selectedItems.addAll(Arrays.asList(savedItems.split(",")));

        int count = this.myList.getAdapter().getCount();

        for (int i = 0; i < count; i++) {
            String currentItem = (String) myList.getAdapter()
                    .getItem(i);
            if (selectedItems.contains(currentItem)) {
                myList.setItemChecked(i, true);
                Toast.makeText(getApplicationContext(),
                        "Current Item: " + currentItem,
                        Toast.LENGTH_LONG).show();
            } else {
                myList.setItemChecked(i, false);
            }

        }
    }
}

private void ClearSelections() {
// user has clicked clear button so uncheck all the items
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myList.setItemChecked(i, false);
    }
// also clear the saved selections
    SaveSelections();
}

private void SelectAllSelections() {
// user has clicked clear button so uncheck all the items
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myList.setItemChecked(i, true);
    }
// also clear the saved selections then uncomment the below line.
// SaveSelections();
}

protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);

    try{
        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        /*if(intent != null){
            startActivity(intent);
        }*/
    }catch(ActivityNotFoundException e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }catch(Exception e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
    ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
    for(ApplicationInfo info : list){
        try{
            if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
                appList.add(info);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return appList;
}

private class LoadApplications extends AsyncTask<Void, Void, Void>{
    private ProgressDialog progress = null;

    protected Void doInBackground(Void... params){
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
        return null;
    }
    protected void onPostExecute(Void result){
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(result);
    }
    protected void onPreExecute(){
        progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
        super.onPreExecute();
    }
}
}
4

1 回答 1

0

在你的情况下,我会使用 DataBase 而不是 SharedPreferences 但如果你更喜欢这个,我会使用另一种方法。循环浏览您的列表并单独保存每个字符串,并带有一些索引。

int i = 0;
sharedPreferences.putInt("list_size", selectedItems.size());
for(String s: selectedItems) {
    sharedPreferences.putString("item_" + i, );
    i++;
}

读书也一样

int readSize = sharedPreferences.getInt("list_size", 0);
for(int i = 0; i < readSize; i++) {
    myList.add(sharedPreferences.getString("item_" + 1, null);
}

它可能是字符串的解决方案。如果您有对象,请考虑对文件进行数据库或事件序列化。

PS您在保存整个列表时阅读的是某种内部哈希索引。因此,您正在保存由 toString() 函数创建的列表的表示。它不一定是正确的表示。

于 2015-10-09T13:46:47.900 回答