jeudi 8 octobre 2015

Unable to see the checkbox beside my list?

I have created a project based on this reference https://www.youtube.com/watch?v=luxE7oEKiic and i am trying to combine it with this reference http://ift.tt/1WQcl6t. However, i have encountered an issue that the checkbox did not appear beside my list of installed application. One of the file which i did not include is array.xml and in my coding there is this line which require the file. I made the following changes to this instead. ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice); // i removed getResources().getStringArray(R.array.Mobile_OS) Is that the reason why i am facing this issue? What should i do to rectify this problem? Kindly advise. Thanks.

public class MainActivity extends ListActivity{

private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppAdapter listadapter = null;

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(),
                        "Curren 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();
    }
}
}




Aucun commentaire:

Enregistrer un commentaire