0

I'm doing a grocery list app and i want all the list to be sort by department (Meat, fruit, bakery etc.). I try to do the first list which is the fruit list everything is working i can add item the checkbox is there i can use it no problem. the problem is that when i check items and i press the previous button on the phone and then i click back on the button to access the fruit list the items are not checked anymore. I use the android.R.layout.simple_list_item_multiple_choice for the checkbox.

I try to use onSaveinstance but i cant get it to work ...i would like to save it without using SQl now my array are save in a file for my list. I eard about shared preference but i'm not sure its what i'm looking for and i would need some help to understand how to use it.

This is the class that call the activity where the fruit list is displayed with the checkbox:

 public class SelectDoActivity extends AppCompatActivity {

/*Set the instance variable*/
private ImageButton btn_FruitList;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_select_do);

  btn_FruitList = (ImageButton) findViewById (R.id.btn_FruitList);

    /*Create the method that call the activity*/
    btn_FruitList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openFruitList_Activity();
        }
    });
}
    public void openFruitList_Activity() {
        Intent intent = new Intent (this, FruitList_Activity.class);
        startActivity(intent);

    }

}

This is the class that display the fruit list with the check mark.

public class FruitList_Activity extends AppCompatActivity   {

private ListView fruitsList;
private ArrayAdapter<String> adapterFruit;
private Button btn_Delete;
private Button btn_SelectAll;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_fruit_list_);

    fruitsList = findViewById(R.id.list_Fruits);
    fruitsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    btn_Delete = findViewById (R.id.btn_delete);
    CreateActivity.itemsFruit = FileHelper.readData(this);

    adapterFruit = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, CreateActivity.itemsFruit);
    fruitsList.setAdapter(adapterFruit);



    /*button to Remove items*/
    btn_Delete.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {

            SparseBooleanArray checkedItemPositions = fruitsList.getCheckedItemPositions();
            int itemCount = fruitsList.getCount();

            for(int i=itemCount-1; i >= 0; i--){
                if(checkedItemPositions.get(i)){
                    fruitsList.setItemChecked(i,true);
                    adapterFruit.remove(CreateActivity.itemsFruit.get(i));
                    FileHelper.writeData(CreateActivity.itemsFruit, FruitList_Activity.this );
                }
            }
            adapterFruit.notifyDataSetChanged();

        }
    });


}

}

I've been stuck with this since 2 weeks i would we really like some precious help i'm ne to java and android so thanks for taking the time to explain how to fix this.

Joel D
  • 39
  • 7
  • You could get the position of checked items and [store positions](https://stackoverflow.com/a/15201430/9968399) in `onPause()` of your activity. Then in `onCreate()` you could retrieve the checked item positions and using a `for` loop, check if current loop position matches the correspondent saved integer position, if yes, then mark that item as checked – Taseer Jul 28 '19 at 19:23
  • Why do you want to save them in a file? Save them as a json or as an array in Shared Preferences – Ahmad Sattout Jul 28 '19 at 19:24
  • @AhmadSattout i thoug about saving them in an array but since my checkbox are from android.R.layout.simple_list_item_multiple_choice i'm not sure how i can refer to them can you help me with that ? – Joel D Jul 28 '19 at 20:31
  • @TaseerAhmad Can you explain me how i can find the position of the checked item when i'm using android.R.layout.simple_list_item_multiple_choice ? thanks – Joel D Jul 28 '19 at 20:33

0 Answers0