简体   繁体   English

如何保存选中的列表视图项

[英]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. 我正在使用Listview使用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? 问题是当我检查项目并转到上一页并返回到列表时,我的项目不再受检查了。我想使用除sharedpreference之外的其他方法,那该怎么办?我该怎么做?

I try to use SparseBooleanArray but it didnt work i probably use it wrong. 我尝试使用SparseBooleanArray,但没有成功,我可能使用了错误的代码。 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? 由于我使用了上一个电话按钮,并且在单击了调用列表活动的按钮后,我应该在onCreate方法中使用某些内容,但是我不确定是什么以及如何使用?

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. 我也尝试使用oninstanceResore方法,就像有人建议我一样,但我不希望它变薄,让我在活动2上说即时消息,这是我的项目列表,其中某些项目已选中,我决定回去用手机做其他事情上一个按钮,然后使用我的应用程序中的按钮返回活动2 ...我希望我的物品仍处于选中状态。 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. 您能指导我如何做到这一点吗?我只是从Java入手,所以请向我解释,以便我理解。 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. 您需要使用列表的信息创建一个Bundle,在调用第二个活动之前,您需要在该Bundle中设置数据,当您回到主活动时,只需检查该Bundle,如果null为null则不同因为您需要加载数据。

I Hope this cal help you :D 我希望这个校准对您有帮助:D

this is saved Instance() 这是保存的Instance()

also, this cal help you when you have onConfigurationChanged() 同样,当您拥有onConfigurationChanged()时,此校准也会为您提供帮助

Save your adapterFruit using onSaveInstanceState 使用onSaveInstanceState保存您的adapterFruit

    @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 返回活动时,将执行onRestoreInstance


    @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);

                }
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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