So basically I have two tabs in a ViewPager
and TabLayout
to store two fragments in a tab view.
Thing is, I am using a card view
in a recycler view
and the card view
has like a Like Button
. Basically when the user clicks the button, it should appear on the next tab - but it doesn't. It is only when the user comes out of the entire page and goes back onto it and selects that tab does the selected card view
is displayed. I checked my database and it is doing what it should be, but it is the recycler view
that doesn't update.
This is my code for liking
the contents in a card view
:
IN CUSTOM ADAPTER CLASS
holder.likebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Database database = new Database(fragment.getContext());
database.likeContent(userID, contentID, name);
}
});
DATABASE CLASS
public void likeContent(int userID, int contentID, String name){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_ID, userID);
contentValues.put(CONTENT_ID, contentID);
contentValues.put(NAME, name);
sqLiteDatabase.insert(DB_TABLE_NAME, null, contentValues);
}
BACKGROUND CLASS
public void showFavourites(final ArrayList<Integer> likes){
theURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
requestQueue = Volley.newRequestQueue(fragment.getActivity());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(theURL, new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray jsonArray) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
...
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
requestQueue.add(jsonArrayRequest);
}
SECOND TAB CLASS
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.likes, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rlikes);
recyclerView.setHasFixedSize(true);
list = new ArrayList<UserInfo>();
adapter = new CAdapter(list, SecondTab.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
conditionNames = new ArrayList<>();
Background = new Background(SecondTab.this, list, adapter);
inputs = new ArrayList<>();
populate();
return rootView;
}
public void populate(){
background.showFavourites(inputs);
}
So after I click the like
button in the Custom Adapter Class
it updates in the database
but it doesn't update the recyclerview
in the second tab.
EDIT: My pager adapter:
public class TabAdapter extends FragmentPagerAdapter{
String tabNames[] = new String[] {"Current", "Likes"};
Context context;
public PodcastGUITabAdapter(FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new CurrentTab();
case 1:
return new LikesTab();
default:
return null;
}
}
@Override
public int getCount() {
return tabNames.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
}
What am I missing?