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:
toolbarButtonsAction (method 1)
The inner class
- Generate Data
- Perform Click automatically :Trigger method 2 in the fragment to refresh the
ListView
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