Good day.
I am building an Android Application. In my main activity, I have a FragmentPageAdpater
and what I basically do is I have this reusable Fragment class that I instantiate three times.
My fragment contains data for three different "pages". I use the current "Active Tab" in my action bar to be able to figure out and filter which data I should show the user. The data is basically a list (dogs, cats, birds) and the user can select a breed. The dog fragment starts first and when the user selects one, my application highlights the selected row and proceeds to the next one (cat fragments). The process repeats until the user is able to select a breed from all three categories.
In order to highlight the user selection, I do this in my onItemClickListener
in my ListView
view.setBackgroundColor(Color.GREEN);
where view
is the View view
argument in the onItemClickListener
function.
However, what happens is when I get to the birds fragment and select then scroll/swipe back to the dog fragment, the green highlight disappears.
What I did try was to have an int to mark which breed index was selected, and then on the Fragment's onResume
, fetch the appropriate int marker and then get my listView, get the child at that row, and then set the background colour. However, I am encountering a NullPointerException in my block of code as such:
This is my onResume function in the Fragment:
@Override
public void onResume() {
super.onResume();
//I get my MainActivity/the base one, get the view pager, and get the current item
int index = ((MainActivity)getActivity()).mViewPager.getCurrentItem();
Log.d("hi", "index is = " + index);
switch(index){
case 0:
//dogSelected is the index of the selected item in the dogFragment
if(((MainActivity)getActivity()).dogSelected > -1){
Log.d("hi","selected dog = " + ((MainActivity)getActivity()).dogSelected);
//I reinitialize the listView
listViewBreeds = (ListView) rootView.findViewById(R.id.listViewBreeds);
adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, DogBreeds);
//DogBreeds is an ArrayList of DogBreeds
listViewBreeds.setAdapter(adapter);
//check for null
if(listViewBreeds == null){
Log.d("hi", "hello, lvn NULL");
}
int pos = ((MainActivity)getActivity()).dogSelected; // + 1;
listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);
}
break;
case 1:
//same as case 0
}
}
As you can see in my code, during the onResume of the Fragment, I check which fragment index is active so I know which list to look up in my switch-case statement. The index logged are correct. Then based on the index, I then look up my lists and the index saved in the base activity. The selected dog (or cat) is correct. After that I reinitialize the listView to make sure it's not null (the log statement does not show up). I then get the listView, get the child at the selected position and then set the background colour.
However, I am getting a NullPointer at the line:
listViewBreeds.getChildAt(pos).setBackgroundColor(Color.GREEN);
and I don't know which part of that is null.
Can anyone help me with this issue? I have a feeling I'm overlooking something simple and I can't figure it out.
Thank you for any help.