HGamble
Lurker
My app uses a ViewPager to display quotations about certain topics. Each array of quotations is in a separate class, and the array is passed to the ViewPager from that class. The toolbar is also set in that class. The toolbar has a heart icon which when clicked adds the quotation to a ArrayList in a favourites activity. That part works fine. What I can't figure out is how to change the heart icon so that it is red after the quotation has been added to the favourites list. How can I access the toolbar from inside the PageViewer class?
Here is where I have the list of quotes about love, and where the toolbar is set.
Here is the code for my ViewPager. I need to access the Toolbar from here.
Here is where I have the list of quotes about love, and where the toolbar is set.
Java:
public class Love extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);
// String arrays with verses and corresponding references
quotes = new String["quotation 1", "quotation 2"...] {//list of quotations};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(Love.this, quotes);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
Here is the code for my ViewPager. I need to access the Toolbar from here.
Java:
public class ViewPagerAdapter extends PagerAdapter {
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] quotes) {
this.context = context;
this.quotes = quotes;
}
public ViewPagerAdapter(){}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView textquotes;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
// Code to locate the TextView in viewpager_item.xml
// Code to capture position and set to the TextView
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
// This is my problem:
if (Favourites.favouritesArrayList.contains(quotes[position])){
//How can I change the icon in the activity from here?
})
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}