简体   繁体   中英

How to save listview item checked

I'm using a listview to display grocery items with checkmark using android.R.layout.simple_list_item_checked. The problem is when i check an item and go to the previous page and comeback to the list my item is not checked anymore..i would like to use something else than sharedpreference is that possible and how can i do that?

I try to use SparseBooleanArray but it didnt work i probably use it wrong. Since i use the previous phone button and after i click on the button that call my list activity i should use something in my onCreate method but i'm not sure what and how?

I also try to use oninstanceResore method like some people suggest me but i dont thin it what i'm looking for let say im on the activity 2 which is my item list with some item checked i decide to go back do something else using the phone previous button and then come back to the activity 2 using the button in my apps ...i want my item to still be checked. Can someone help me on how to do this i will be very grateful…

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_checked, CreateActivity.itemsFruit);
        fruitsList.setAdapter(adapterFruit);




        /*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();

            }
        });



    }
}

Can you guide me on how i can do that i'm just starting with java so please explain me so i can understand. thanks!

You need to create a Bundle with the information of the list, and before calling the second activity, you'll set the data inside that bundle, and when you came back to the main activity just check the bundle and if is different at null is because you need to load the data.

I Hope this cal help you :D

this is saved Instance()

also, this cal help you when you have onConfigurationChanged()

Save your adapterFruit using onSaveInstanceState

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putSerializable("adapter",(Serializable) adapterFruit);
    }

The onRestoreInstance executes when you go back to the activity


    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if(saveInstanceState != null){
               adapterFruit = (ArrayAdapter<Adapter> )savedInstanceState.getSerializable("adapter");
        }

    }

Hope it helps.

you can do like this

holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.checkBox.isChecked()==true) {
                      holder.checkBox.setChecked(true);
                      model_image.get(position).setSelected(true);
                }
                else
                {
                    holder.checkBox.setChecked(false);
                    model_image.get(position).setSelected(false);

                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM