如何根据SharedPreferences中的值设置simple_list_item_multiple_choice中的复选框

问题描述

我有一个ListView,我正在使用Arrayadapter从firebase数据库添加值。现在我想使用simple_list_item_multiple_choice,它将具有复选框,并且我想存储复选框的状态在SharedPreferences中,以便当用户销毁应用程序然后重新打开复选框时仍保持选中状态。但是由于我是Android新手,所以我不知道如何在ArrayAdapter中设置SharedPreferences值。我不想将状态存储在数据库中。我只想存储复选框状态并在Listview中检索它。

现在我正在做的是使用SharedPreferences OnDestroy方法将复选框存储在SparseBooleanArray中,然后在OnCreteView方法上检索它。这是我的代码

OnDestroy方法

    @Override
public void onDestroy() {
    super.onDestroy();
    int len = listview.getCount();
    SparseBooleanArray checked = listview.getCheckedItemPositions();

    for (int i = 0; i < len; i++){
        if (checked.get(i)) {

            String item = uploads.get(i);

            SharedPreferences pref =getActivity().getSharedPreferences("Checkdata",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor =pref.edit();
            editor.putString("name"+i,item);
            editor.commit();

        }else {

        }

    }
    Toast.makeText(getActivity(),"Destroyed",Toast.LENGTH_SHORT).show();
}

OnCreateView方法

OnCreateView方法中,我在其中调用checkConnection()方法,在其中我称为ViewListName()方法,在其中我得到了SharedPrefrences。

 private void ViewListName() {

        progressDialog =new ProgressDialog(getActivity());
        progressDialog.setTitle("Loading .. ");
        progressDialog.setMessage("Loading Data Please Wait");
        progressDialog.show();

       progressDialog.setCanceledOnTouchOutside(false);

    databaseReference = FirebaseDatabase.getInstance().getReference("Books");

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot postsnapshot : dataSnapshot.getChildren()){
                String uploadPD = postsnapshot.getKey();
                uploads.add(uploadPD);
            }
            String[] uploadsstr = new String[uploads.size()];
            for(int i=0;i<uploadsstr.length;i++){
                uploadsstr[i] = uploads.get(i);
            }
            SharedPreferences pref =getActivity().getSharedPreferences("Checkdata",Context.MODE_PRIVATE);
           for (int i = 0; i < uploadsstr.length; i++){

             String val = pref.getString("name"+i,"");
             Log.w("Value",val);
            }


            if(uploadsstr.length == 0){
                nodata.setVisibility(View.VISIBLE);
                listview.setVisibility(View.INVISIBLE);
                Toast.makeText(getContext(),"No data",Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }else{
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_multiple_choice,uploadsstr);
                listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                listview.setAdapter(adapter);
                progressDialog.dismiss();
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            checkConnection();

        }
    });

}

checkConnection()

  public void checkConnection(){
    ConnectivityManager connectivityManager =(ConnectivityManager) getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

    if(null != activeNetwork){
       // Toast.makeText(getActivity(),"Internet On",Toast.LENGTH_SHORT).show();
        ViewListName();
    }else{
        InternetDailogBox Box2 = new InternetDailogBox();
        Box2.show( getActivity().getSupportFragmentManager(),"No Connection");
    }
}

onCreateView

  public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    homeviewmodel =
            viewmodelProviders.of(this).get(Homeviewmodel.class);
    View root = inflater.inflate(R.layout.fragment_home,container,false);



    listview =(ListView) root.findViewById(R.id.lv);
    nodata =root.findViewById(R.id.nodata);
    checkConnection();



  
    uploads = new ArrayList<>();
    listview.setonItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
            String  uploadPDF = uploads.get(position);

            Toast.makeText(getActivity().getApplicationContext(),""+uploadPDF,Toast.LENGTH_SHORT).show();
            Intent i = new Intent(getActivity().getApplicationContext(),ChapterList.class);
            i.putExtra("chp_name",uploadPDF);
            startActivity(i);
        }
    });
   return root;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...