如何解决如何根据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 举报,一经查实,本站将立刻删除。