my question is is there a way to save the SpamCount of a ReciclerView in SharedPreferences? I try to make a List that can change the view from a "List mode" to "Grid mode" and save that information in SharedPreferences so that the "List View" will remain after killing the app. The example of what I am trying to do would be the following ... example of what i try to do on button click
And the code that I try to change is the following ...
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static RecyclerView recyclerView;
private ReciclerAdapter reciclerAdapter;
ImageView viewGridS, viewListS;
SharedPreferences vSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recicler_main);
vSettings = this.getSharedPreferences("Vision", 0);
notesRecyclerView = findViewById(R.id.recycler_view);
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
viewListS = findViewById(R.id.viewList);
viewGridS = findViewById(R.id.viewGrid);
@Override
public void onClick(View v) {
SharedPreferences vSettings = this.getSharedPreferences("Vision", 0);
SharedPreferences.Editor viEdit = vSettings.edit();
switch (v.getId())
{
case R.id.viewList:
viewGridS.setVisibility(View.GONE);
viewListS.setVisibility(View.VISIBLE);
viEdit.putString("Vision",listView());
viEdit.apply();
break;
case R.id.viewGrid:
viewListS.setVisibility(View.GONE);
viewGridS.setVisibility(View.VISIBLE);
listWiew();
viEdit.putString("Vision",gridView());
viEdit.apply();
break;
}
});
}
public int gridView(){
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
return null;
}
public int listView(){
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
return null;
}
}
When trying to start the Activity the RecyclerView does not show anything and the button does not work. if someone could help me I would appreciate it in advance ... thanks
You have done couple of things wrong here.
I will try to accomplished kind similar end result which you are looking for, a simple button which is switching layout on click. Code as follows -
MainActivity.java
activity_main.xml
RecycleViewAdapter.java
recycle_item_layout.xml
Above code will give you following result -
Happy Coding !