0

I have a bug in my code, but I couldn't figure out WHY because sometimes it works sometime it doesn't. Hope you can help me point out the problem. Here is issue:

In a fragment, I setup a Gridview + ListView. When user click on a gridcell (the cell highlighted) then I display info of that cell is on the ListView (This work just fine if user perform physical click action). I also have buttons to regenerate data of for the GridView and will perform a click AUTOMATICALLY. The data of GridView generated correctly and a cell is highlighted as expected (This is performed in the inner class), but the ListView is not rendered(This is performed in fragment). I did debug and lines are executed as expected but nothing happened. However sometime, I click on same button with different input data and it works.

Below are sample flow of my class when a tool bar button clicked:

  1. toolbarButtonsAction (method 1)

  2. The inner class

    • Generate Data
    • Perform Click automatically :Trigger method 2 in the fragment to refresh the ListView
  3. The refreshListView method is called and every single line of code is executed as expected.

    public class GridListViewFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { //General Lifecycle setup. //Too much to list here.....

    m_listView = (ListView)getView().findViewById(R.id.eventsList);
    
    }
    
    //Method 1
    View.OnClickListener toolbarButtonsAction = new View.OnClickListener() 
    {
        // Each view will return different input data each time clicked
        // This will call the inner class GridCellAdapter
        // The inner class will generate data to display on grid cell
        // If necessary, performClick() automatically 
    
    }
    
    //Method 2
    public void refreshListView()
    {
        //This method will be triggered from ome method in the inner class
    
        // I'm sure the Arraylist is valid
        if (anArrayList.size() == 0)
    {
        String[] notAvail = new String[]{"NotAvail String."};
        ArrayAdapter<String> nullAdapter = new ArrayAdapter<String>
        (getActivity(),android.R.layout.simple_list_item_2, android.R.id.text2, notAvail);          
        m_listView.setClickable(false);
        m_listView.setEnabled(false);
        m_listView.setAdapter(nullAdapter);
    
    }else{
    
    
        ListViewAdapter lviewAdapter = new ListViewAdapter(getActivity(),anArrayList);
        lviewAdapter.notifyDataSetChanged();
        // Set adapter for ListView
        m_listView.setClickable(true);
        m_listView.setEnabled(true);
        m_listView.setAdapter(lviewAdapter);
    
    }
    
    
    }
    
    
    //This is inner class
    
    public class GridCellAdapter extends BaseAdapter implements OnClickListener
    {
    
    //Other methods....
    
     @Override
     public void onClick(View view)
      {
          //Generate data for the anArrayList
          //Call the refreshListView in the fragment.
          refreshListView();
      }
    } 
    

Update: It looked like the content of the ListView is disappeared, but the ListView is still showing. I checked by set color to background of the ListView.

Update2: getView() in ListViewAdapter in some case is not get called. This is issue

JHHoang
  • 653
  • 1
  • 8
  • 22
  • Any helps please. Im still struggling with this problem. – JHHoang Mar 01 '13 at 16:03
  • Just a quick question, what are you showing in the list? TextViews? Could it be that you have a \n symbol in the beginning of the string? If so, it might disappear. What if you insert all the TextViews with the same data and see if everything shows? If everything is showing with the same text then I am certain your string contains a new row symbol. Please tell me if you did not understand because I might have told a bit hard to understand. – Simon Zettervall Mar 05 '13 at 16:16
  • I try to show a list view, if data available for a selected grid cell then show that data in the list view. Otherwise I create new string adapter that show only 1 string "Not Avail String" and set that adapter to the same list view (tried with separated list view but didnt work). There is no "\n". Because this issue happened in both cases (data not avail. and avail.) – JHHoang Mar 05 '13 at 16:22
  • Ok, do you ever insert a null string or ""? I was thinking maybe the data you got did not contain anything. – Simon Zettervall Mar 05 '13 at 16:24
  • And it's not always happened. The inner class job is return data array for selected grid cell. I did debug the data array is valid as expected. – JHHoang Mar 05 '13 at 16:24
  • Instead of just logging out things you should try to populate the listview with the same data. That way you can see if something is not right. You KNOW you have the right data but you cannot be sure it SHOWS the right data. – Simon Zettervall Mar 05 '13 at 16:26
  • For the case, there is not data avail. I clear the data array by anArrayList.Clear(). When it enter method refreshListView() the if statement is true. For this it is obviously I just populate a string to the list view. However, SOMETIMES that string is not showing but the list view – JHHoang Mar 05 '13 at 16:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25614/discussion-between-simon-zettervall-and-jhhoang) – Simon Zettervall Mar 05 '13 at 16:31

1 Answers1

0

After having a chat discussion we came to a resolution. Let me qoute:

The issue is the getView() in listViewAdapter is not getting called sometime due to listView and its activity TabHost layout. Now I change android:layout_height="fill_parent" in main_activity and list view layout to android:layout_height="wrap_content"

So all along there was a layout error which indeed are hard to find sometimes and can be very mean.

Simon Zettervall
  • 1,764
  • 1
  • 15
  • 31