thathashd
thathashd

Reputation: 1022

Shared Preferences multiple choice ListView

I am developing an application that enables management of tasks in which I have two activities. The first activity is a simple listview, which is presented a list of users. The second activity is a multiple choice listview, which shows the tasks associated with that user. I managed to reach my first goal which was to store the state of the checkbox, using shared preferences. My problem is that when I save the state of checkboxs for a user, when you see the tasks of another user, these are no longer checked. How can i solve my problem.

This is how I fill the list:

    mList = (ListView) findViewById(android.R.id.list);      
    final DbAdapter db = new DbAdapter(this);
    db.open();  
    data = db.getAllTasks(getIntent().getExtras().get("nameUser").toString());
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,data);
    mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mList.setAdapter(adapter);

This is the way i select the user i want to show the tasks:

        mList.setOnItemClickListener(new OnItemClickListener() {     
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            final String text = (String) mList.getItemAtPosition(position);
        }
    });

And this is the methods i use to save the state using SharedPreferences:

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

    SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);

    if (settingsActivity.contains(SETTING_DB)) {
        String savedItems = settingsActivity
                .getString(SETTING_DB, "");

        this.selectedItems.addAll(Arrays.asList(savedItems.split(",")));
        int count = this.mList.getAdapter().getCount();

        for (int i = 0; i < count; i++) {
            String currentItem = (String) this.mList.getAdapter()
                    .getItem(i);
            if (this.selectedItems.contains(currentItem)) {
                this.mList.setItemChecked(i, true);
            }

        }

    }
}


private void SaveSelections() {

    // save the selections in the shared preference in private mode for the user

    SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = settingsActivity.edit();

    String savedItems = getSavedItems();

    prefEditor.putString(SETTING_DB , savedItems);

    prefEditor.commit();
}



private String getSavedItems() {
    String savedItems = "";

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

    for (int i = 0; i < count; i++) {

        if (this.mList.isItemChecked(i)) {
            if (savedItems.length() > 0) {
                savedItems += "," + this.mList.getItemAtPosition(i);
            } else {
                savedItems += this.mList.getItemAtPosition(i);
            }
        }

    }
    return savedItems;
}

My regards.

Upvotes: 1

Views: 1568

Answers (1)

BonanzaDriver
BonanzaDriver

Reputation: 6452

You'll need to include, as part of your Preferences, the user those particular tasks are assoicated with. It's not ideal to use the SharredPreferrences for this purpose (better to simply use the database instead IMO), but in the end you need to find the selected items to a particular user, then when the user is selected you can see "which" items where selected.

Upvotes: 2

Related Questions