0

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 viewargument 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.

Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • findviewbyid() will often return a null if you're looking in the layout or view. It's possible that rootview has not been inflated yet in the onResume() call, making it impossible to find an element that is part of it. – Alex-v-s May 05 '15 at 01:39
  • So how do I ensure that `rootView` is inflated? rootView looks like `rootView = inflater.inflate(R.layout.fragement_nominee_page, container, false);` and I don't think I can initialize it through the onResume method since the container is from the onStart method. – Razgriz May 05 '15 at 01:46
  • Well before we try to find a solution, make sure that *this is the problem* A simple if(listViewBreeds==null)Log.d("debug", "yep definitely this"); will do. – Alex-v-s May 05 '15 at 01:51
  • i dont think `ListView.getchildAt();` is that right way to go, why you are getting the NPE im still not sure but why don't you replace `rootView` with `getView()` and also you might want to check this indirect [post](http://stackoverflow.com/questions/30008728/android-setvisibility-not-working-in-progressbar/30015903#30015903) to your question it will give you the right way to go – Elltz May 05 '15 at 02:16
  • `listViewBreeds` is not null, it would be throw the exception at the `listViewBreeds.setAdapter(adapter);` line. I would guest that `getChildAt(pos)` is returning a null object. – Julio_oa May 05 '15 at 02:34
  • @NeoMime did you read the question? I have that if statement in my code and the log in that statement does not appear. Meaning that the listView is not null. – Razgriz May 05 '15 at 04:02
  • @Elltz, I'll check the link, thank you. – Razgriz May 05 '15 at 04:04
  • I'm getting a null pointer at the `.getChild()` part. – Razgriz May 05 '15 at 04:28
  • were you refering to me? – Elltz May 06 '15 at 19:45

1 Answers1

0

I actually got it to work already. The solution was to use a CustomListView Adapter and then create an object for the row entry. That object has a boolean isSelected, then in the Custom ListView Adapter, the background is set to a different color or is set back to white depending on the isSelected variable.

Razgriz
  • 7,179
  • 17
  • 78
  • 150