last day (14 days later) » 

10:25 AM
0
A: Returning to same recycler view position after coming from other activity

Abdul KaweeYou can do this First save the position of recycler view in shared prefernces SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("SNOW_DENSITY",mSnowDensity); editor.commit(); And then when you come back ju...

 
Sorry! Didnt get you completely. What values i have to give in editor.putInt ? mSnowDensity is which value?
Not working. I tried this. In onResume I am fetching the value
 
its the position of recycler view
what re you getting?? when you are fetching the value
 
yes i am giving position by position=getAdapterPosition...
 
and when getting from shared preferences what are you getting?
 
yes its in on apuse only
On pause only
but position=getAdapter position is always -1
its showing
 
10:33 AM
then your are not getting position properly
your get position should be the position of item click
 
but that is the only way to get the position ryt?
oh.. ok
 
share the code how you are getting the position
 
oh.. ok
U adapter and activity also?
 
you are getting the position of item in activity?
then share the activity coed
code
 
yes now i am getting position
but now i want to show the recycler with that position
time_line_recycler.scrollToPosition(Integer.parseInt(sharedPrefs.getPref("adapte‌​rPos")) - 1);
 
10:47 AM
great
simple use the llast line in answer
 
this is not working
 
whats the error you are getting?
 
recycler is showing from position 0
i am not getting any error
 
you have saved the position ins tring form?
 
yes
 
10:48 AM
just log or toast the value you are getting from shared preferences
 
in log position is printing properly
05-17 16:16:30.205 2868-2868/com.appiness.sarvam E/adapter pos: adapter pos10
see position getting properly
 
again check are you stroing value in string or int form
 
no string format
sharedPrefs.setPref("adapterPos", String.valueOf(position));
this is how i am saving position
 
no you are not saving value properly
save the value in the way is posted in the answer
 
but its printing know
 
10:52 AM
show the complete code of shared prefernces
 
ok
in activity
@Override
protected void onResume() {
super.onResume();
// SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME",0);
Log.e("adapter pos","adapter pos"+ sharedPrefs.getPref("adapterPos"));
// int val=sharedPrefs.getPref("rvPos");
// int snowDensity = settings.getInt("SNOW_DENSITY", 0);
time_line_recycler.scrollToPosition(Integer.parseInt(sharedPrefs.getPref("adapterPos")) - 1);
}
@Override
protected void onResume() {
super.onResume();
Log.e("adapter pos","adapter pos"+ sharedPrefs.getPref("adapterPos"));
time_line_recycler.scrollToPosition(Integer.parseInt(sharedPrefs.getPref("adapterPos")) - 1);
}
this is my on resume
 
no simply get the result in variable
 
In adapter On click i am giving sharedPrefs.setPref("adapterPos", String.valueOf(position));
ok
 
and use
putString or putInt
and getString or getInt methods
the right way of doing things
 
i didnt get you
here also i am getting position
 
10:58 AM
ok just do it by the way i did in answer
 
okay i am trying that
@Override
protected void onResume() {
super.onResume();
Log.e("adapter pos","adapter pos"+ sharedPrefs.getPref("adapterPos"));
// time_line_recycler.scrollToPosition(Integer.parseInt(sharedPrefs.getPref("adapterPos")) - 1);
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0);
time_line_recycler.scrollToPosition(snowDensity - 1);
}
now this is my On resume
is it correct?
 
and you are putting int also?
 
This is in Adapter
SharedPreferences settings = mContext.getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY",position);
editor.commit();
but not working
showing 0th position item only
 
its not showing position?
and your old way was showing the result?
 
my old way getting the position properly
and the new way also returning position
 
11:06 AM
let me test one more thing
 
ok
 
mAdapter = new AudioTestAdapter(listCategory2, AudioTestClass.this);

mRecyclerView.setAdapter(mAdapter);
mRecyclerView.scrollToPosition(firstCounter - 1);
do this
first have the adapter refernce in on resume
then set and then scroll
its working
 
oh ok
let me try
not working
 
11:39 AM
any error?
 
no error
not going to particular position
 
are you using recycler view or list view?
 
11:55 AM
recycler
view
@Override
protected void onResume() {
super.onResume();


SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0);
Log.e("adapter pos","adapter pos"+ snowDensity);
tModel = new ArrayList<>();
timelineAdapter = new TimelineAdapter(tModel);
getTimeline();
time_line_recycler.setAdapter(timelineAdapter);
time_line_recycler.scrollToPosition(snowDensity - 1);
}
this is my onResume now
 
what is getTimeline(); method?
 
there i am getting vaues from API
and adding to list
i commeted that line even though not working
 
ok try this
in on resume do complete task
 
means?
 
i.e get values, then create new adatper and ad values
then use scroll on it
 
11:59 AM
oh
now also same thing right?
just getting values by using a method
 
yess
but with new objects
 
oh noo.. that method is very big data
 
try changing scroll to position
static
give it some static number and test weather its working or not
 
ok
not working
 

last day (14 days later) »